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