载入中...
搜索中...
未找到
Sha256.cpp
浏览该文件的文档.
1#include "cmdline/Sha256.h"
2
3#include <algorithm>
4#include <cstring>
5#include <iomanip>
6#include <sstream>
7
9
10namespace {
11
12constexpr std::array<uint32_t, 64> kConstants = {
13 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
14 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
15 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
16 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
17 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
18 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
19 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
20 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u};
21
22constexpr uint32_t rotateRight(uint32_t value, unsigned count) { return (value >> count) | (value << (32u - count)); }
23
24} // namespace
25
26void Sha256::transform(const uint8_t* block) {
27 std::array<uint32_t, 64> words{};
28 for (std::size_t i = 0; i < 16; ++i) {
29 const std::size_t offset = i * 4;
30 words[i] = (static_cast<uint32_t>(block[offset]) << 24u) | (static_cast<uint32_t>(block[offset + 1]) << 16u) |
31 (static_cast<uint32_t>(block[offset + 2]) << 8u) | static_cast<uint32_t>(block[offset + 3]);
32 }
33 for (std::size_t i = 16; i < words.size(); ++i) {
34 const uint32_t s0 = rotateRight(words[i - 15], 7) ^ rotateRight(words[i - 15], 18) ^ (words[i - 15] >> 3u);
35 const uint32_t s1 = rotateRight(words[i - 2], 17) ^ rotateRight(words[i - 2], 19) ^ (words[i - 2] >> 10u);
36 words[i] = words[i - 16] + s0 + words[i - 7] + s1;
37 }
38
39 uint32_t a = state_[0];
40 uint32_t b = state_[1];
41 uint32_t c = state_[2];
42 uint32_t d = state_[3];
43 uint32_t e = state_[4];
44 uint32_t f = state_[5];
45 uint32_t g = state_[6];
46 uint32_t h = state_[7];
47 for (std::size_t i = 0; i < words.size(); ++i) {
48 const uint32_t sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
49 const uint32_t choice = (e & f) ^ (~e & g);
50 const uint32_t temp1 = h + sum1 + choice + kConstants[i] + words[i];
51 const uint32_t sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
52 const uint32_t majority = (a & b) ^ (a & c) ^ (b & c);
53 const uint32_t temp2 = sum0 + majority;
54 h = g;
55 g = f;
56 f = e;
57 e = d + temp1;
58 d = c;
59 c = b;
60 b = a;
61 a = temp1 + temp2;
62 }
63 state_[0] += a;
64 state_[1] += b;
65 state_[2] += c;
66 state_[3] += d;
67 state_[4] += e;
68 state_[5] += f;
69 state_[6] += g;
70 state_[7] += h;
71}
72
73void Sha256::update(const char* data, std::size_t size) {
74 totalBytes_ += size;
75 while (size > 0) {
76 const std::size_t count = std::min(size, buffer_.size() - buffered_);
77 std::memcpy(buffer_.data() + buffered_, data, count);
78 buffered_ += count;
79 data += count;
80 size -= count;
81 if (buffered_ == buffer_.size()) {
82 transform(buffer_.data());
83 buffered_ = 0;
84 }
85 }
86}
87
88std::string Sha256::finish() {
89 const uint64_t totalBits = totalBytes_ * 8u;
90 buffer_[buffered_++] = 0x80u;
91 if (buffered_ > 56) {
92 std::fill(buffer_.begin() + buffered_, buffer_.end(), 0);
93 transform(buffer_.data());
94 buffered_ = 0;
95 }
96 std::fill(buffer_.begin() + buffered_, buffer_.begin() + 56, 0);
97 for (unsigned i = 0; i < 8; ++i) buffer_[63 - i] = static_cast<uint8_t>(totalBits >> (i * 8u));
98 transform(buffer_.data());
99
100 std::ostringstream out;
101 out << std::hex << std::setfill('0');
102 for (const uint32_t value : state_) out << std::setw(8) << value;
103 return out.str();
104}
105
106} // namespace eve::cmd::sdk::detail
double value
tensor::Graph g
Definition GpuGraph.cpp:7
std::int32_t c
int h
size_t offset
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float f
float d
std::uint32_t count
float size
Definition TreeMesh.cpp:156
std::string finish()
Finish.
Definition Sha256.cpp:88
void update(const char *data, std::size_t size)
Updates .
Definition Sha256.cpp:73