You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sha256.h 6.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright 2020 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. #ifndef MINDSPORE_CCSRC_UTILS_SYSTEM_SHA256_H_
  17. #define MINDSPORE_CCSRC_UTILS_SYSTEM_SHA256_H_
  18. #include <string>
  19. #include <iomanip>
  20. #include <fstream>
  21. #include <memory>
  22. #include <vector>
  23. namespace mindspore {
  24. namespace system {
  25. namespace sha256 {
  26. constexpr int kBitNumber = 8;
  27. constexpr int kDigestSize = 8;
  28. constexpr int kIterationNumber = 64;
  29. constexpr int kMessageBlockLength = 64;
  30. const uint32_t constant[64] = {
  31. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  32. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  33. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  34. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  35. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  36. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  37. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  38. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
  39. inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ ((~x) & z); }
  40. inline uint32_t ma(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (x & z) ^ (y & z); }
  41. inline uint32_t sigma0(uint32_t x) { return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10); }
  42. inline uint32_t sigma1(uint32_t x) { return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7); }
  43. inline uint32_t sigma2(uint32_t x) { return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3); }
  44. inline uint32_t sigma3(uint32_t x) { return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10); }
  45. std::string LoadFilePath(const std::string &path) {
  46. char real_path[PATH_MAX] = {0};
  47. #if defined(_WIN32) || defined(_WIN64)
  48. if (path.size() > PATH_MAX || _fullpath(real_path, path.c_str(), PATH_MAX) == nullptr) {
  49. return "";
  50. }
  51. #else
  52. if (path.size() > PATH_MAX || realpath(path.c_str(), real_path) == nullptr) {
  53. return "";
  54. }
  55. #endif
  56. std::ifstream bin_stream(real_path, std::ios::binary);
  57. if (!bin_stream.is_open()) {
  58. return "";
  59. }
  60. std::string message((std::istreambuf_iterator<char>(bin_stream)), std::istreambuf_iterator<char>());
  61. return message;
  62. }
  63. bool Padding(std::string *message) {
  64. uint64_t bits_message = message->size() * kBitNumber;
  65. const int remains = message->size() % kMessageBlockLength;
  66. // The length of the message needs to be stored in 8 bytes, supplemented at the end of the message.
  67. const int size_append = 8;
  68. const int size_required = kMessageBlockLength - size_append;
  69. const int size_pad = size_required - remains + (size_required > remains ? 0 : kMessageBlockLength);
  70. if (size_pad < 1 || size_pad > kMessageBlockLength) {
  71. return false;
  72. }
  73. message->push_back(0x80);
  74. for (int i = 1; i < size_pad; ++i) {
  75. message->push_back(0x00);
  76. }
  77. for (int i = size_append - 1; i >= 0; --i) {
  78. message->push_back(static_cast<uint8_t>((bits_message >> static_cast<uint32_t>(i * kBitNumber)) & 0xff));
  79. }
  80. return true;
  81. }
  82. bool ProcessInner(const std::string &message, const int &bias, uint32_t *digest, const int &digest_size) {
  83. if (digest_size != 8) { // The number of digests is fixed at 8
  84. return false;
  85. }
  86. uint32_t w[kIterationNumber] = {0};
  87. for (int i = 0; i < 16; ++i) {
  88. w[i] = (static_cast<uint32_t>(static_cast<uint8_t>(message[bias + i * 4]) & 0xff) << 24) |
  89. (static_cast<uint32_t>(static_cast<uint8_t>(message[bias + i * 4 + 1]) & 0xff) << 16) |
  90. (static_cast<uint32_t>(static_cast<uint8_t>(message[bias + i * 4 + 2]) & 0xff) << 8) |
  91. (static_cast<uint32_t>(static_cast<uint8_t>(message[bias + i * 4 + 3]) & 0xff));
  92. }
  93. for (int i = 16; i < kIterationNumber; ++i) {
  94. w[i] = sigma3(w[i - 2]) + w[i - 7] + sigma2(w[i - 15]) + w[i - 16];
  95. }
  96. std::vector<uint32_t> hash(digest_size);
  97. size_t mem_size = digest_size * sizeof(uint32_t);
  98. auto ret = memcpy_s(hash.data(), mem_size, digest, mem_size);
  99. if (ret != EOK) {
  100. return false;
  101. }
  102. for (int i = 0; i < kIterationNumber; ++i) {
  103. uint32_t t1 = w[i] + constant[i] + hash[7] + sigma1(hash[4]) + ch(hash[4], hash[5], hash[6]);
  104. uint32_t t2 = sigma0(hash[0]) + ma(hash[0], hash[1], hash[2]);
  105. for (int j = digest_size - 1; j >= 0; --j) {
  106. if (j == 4) {
  107. hash[j] = hash[j - 1] + t1;
  108. } else if (j == 0) {
  109. hash[j] = t1 + t2;
  110. } else {
  111. hash[j] = hash[j - 1];
  112. }
  113. }
  114. }
  115. for (int i = 0; i < digest_size; ++i) {
  116. digest[i] += hash[i];
  117. }
  118. return true;
  119. }
  120. std::string ConvertToString(uint32_t *input, const int &size) {
  121. std::ostringstream oss;
  122. oss << std::hex;
  123. for (int i = 0; i < size; ++i) {
  124. for (int j = static_cast<int>(sizeof(uint32_t) / sizeof(uint8_t)) - 1; j >= 0; --j) {
  125. uint8_t val = static_cast<uint8_t>((input[i] >> static_cast<uint32_t>(j * kBitNumber)) & 0xff);
  126. oss << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(val);
  127. }
  128. }
  129. return oss.str();
  130. }
  131. std::string Encrypt(const std::string &message) {
  132. uint32_t digest[kDigestSize] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  133. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
  134. for (int i = 0; i < static_cast<int>(message.size()); i += kMessageBlockLength) {
  135. if (!ProcessInner(message, i, digest, kDigestSize)) {
  136. return "";
  137. }
  138. }
  139. return ConvertToString(digest, kDigestSize);
  140. }
  141. std::string GetHashFromString(const std::string &data) {
  142. std::string message = data;
  143. if (message.empty() || !Padding(&message)) {
  144. return "";
  145. }
  146. return Encrypt(message);
  147. }
  148. std::string GetHashFromFile(const std::string &path) {
  149. std::string message = LoadFilePath(path);
  150. if (message.empty() || !Padding(&message)) {
  151. return "";
  152. }
  153. return Encrypt(message);
  154. }
  155. } // namespace sha256
  156. } // namespace system
  157. } // namespace mindspore
  158. #endif // MINDSPORE_CCSRC_UTILS_SYSTEM_SHA256_H_