1 /**
2 * Copyright 2020-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MINDSPORE_CCSRC_UTILS_SYSTEM_SHA256_H_
18 #define MINDSPORE_CCSRC_UTILS_SYSTEM_SHA256_H_
19
20 #include <string>
21
22 namespace mindspore {
23 namespace system {
24 namespace sha256 {
ch(uint32_t x,uint32_t y,uint32_t z)25 inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ ((~x) & z); }
ma(uint32_t x,uint32_t y,uint32_t z)26 inline uint32_t ma(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (x & z) ^ (y & z); }
sigma0(uint32_t x)27 inline uint32_t sigma0(uint32_t x) { return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10); }
sigma1(uint32_t x)28 inline uint32_t sigma1(uint32_t x) { return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7); }
sigma2(uint32_t x)29 inline uint32_t sigma2(uint32_t x) { return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3); }
sigma3(uint32_t x)30 inline uint32_t sigma3(uint32_t x) { return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10); }
31
32 std::string LoadFilePath(const std::string &path);
33
34 bool Padding(std::string *message);
35
36 bool ProcessInner(const std::string &message, const int &bias, uint32_t *digest, const int &digest_size);
37
38 std::string ConvertToString(const uint32_t *input, const int &size);
39
40 std::string Encrypt(const std::string &message);
41
42 std::string GetHashFromString(const std::string &data);
43
44 std::string GetHashFromFile(const std::string &path);
45
46 #ifndef _WIN32
47 std::string GetHashFromDir(const std::string &dir);
48 #endif
49 } // namespace sha256
50 } // namespace system
51 } // namespace mindspore
52 #endif // MINDSPORE_CCSRC_UTILS_SYSTEM_SHA256_H_
53