Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1 | #include "aos/sha256.h" |
| 2 | |
| 3 | #include <iomanip> |
| 4 | #include <sstream> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "absl/types/span.h" |
| 8 | #include "openssl/sha.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | |
| 12 | std::string Sha256(const absl::Span<const uint8_t> str) { |
| 13 | unsigned char hash[SHA256_DIGEST_LENGTH]; |
| 14 | SHA256_CTX sha256; |
| 15 | SHA256_Init(&sha256); |
| 16 | SHA256_Update(&sha256, str.data(), str.size()); |
| 17 | SHA256_Final(hash, &sha256); |
| 18 | std::stringstream ss; |
| 19 | for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { |
| 20 | ss << std::hex << std::setw(2) << std::setfill('0') |
| 21 | << static_cast<int>(hash[i]); |
| 22 | } |
| 23 | return ss.str(); |
| 24 | } |
| 25 | |
| 26 | } // namespace aos |