Loading...
Searching...
No Matches
Sha256.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "Sha256.h"
6
7#include <algorithm>
8#include <array>
9#include <cstdint>
10#include <cstring>
11#include <fstream>
12#include <vector>
13
14namespace gpr {
15
16namespace {
17
18constexpr std::array<std::uint32_t, 64> kRound = {
19 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
20 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
21 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
22 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
23 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
24 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
25 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
26 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
27 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
28 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
29 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
30
31inline std::uint32_t rotr(std::uint32_t x, int n)
32{
33 return (x >> n) | (x << (32 - n));
34}
35
36void compress(std::array<std::uint32_t, 8>& h, const std::uint8_t* block)
37{
38 std::array<std::uint32_t, 64> w{};
39 for (int i = 0; i < 16; ++i) {
40 w[i] = (std::uint32_t(block[4 * i]) << 24) |
41 (std::uint32_t(block[4 * i + 1]) << 16) |
42 (std::uint32_t(block[4 * i + 2]) << 8) |
43 std::uint32_t(block[4 * i + 3]);
44 }
45 for (int i = 16; i < 64; ++i) {
46 const std::uint32_t s0 =
47 rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
48 const std::uint32_t s1 =
49 rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
50 w[i] = w[i - 16] + s0 + w[i - 7] + s1;
51 }
52 auto v = h;
53 for (int i = 0; i < 64; ++i) {
54 const std::uint32_t s1 =
55 rotr(v[4], 6) ^ rotr(v[4], 11) ^ rotr(v[4], 25);
56 const std::uint32_t ch = (v[4] & v[5]) ^ (~v[4] & v[6]);
57 const std::uint32_t t1 = v[7] + s1 + ch + kRound[i] + w[i];
58 const std::uint32_t s0 =
59 rotr(v[0], 2) ^ rotr(v[0], 13) ^ rotr(v[0], 22);
60 const std::uint32_t maj = (v[0] & v[1]) ^ (v[0] & v[2]) ^ (v[1] & v[2]);
61 const std::uint32_t t2 = s0 + maj;
62 v[7] = v[6];
63 v[6] = v[5];
64 v[5] = v[4];
65 v[4] = v[3] + t1;
66 v[3] = v[2];
67 v[2] = v[1];
68 v[1] = v[0];
69 v[0] = t1 + t2;
70 }
71 for (int i = 0; i < 8; ++i)
72 h[i] += v[i];
73}
74
75class Hasher {
76public:
77 void update(const std::uint8_t* data, std::size_t size)
78 {
79 total_ += size;
80 while (size > 0) {
81 const std::size_t take = std::min(size, std::size_t(64) - fill_);
82 std::memcpy(buffer_.data() + fill_, data, take);
83 fill_ += take;
84 data += take;
85 size -= take;
86 if (fill_ == 64) {
87 compress(h_, buffer_.data());
88 fill_ = 0;
89 }
90 }
91 }
92
93 std::string finish()
94 {
95 const std::uint64_t bits = total_ * 8;
96 const std::uint8_t pad = 0x80;
97 update(&pad, 1);
98 const std::uint8_t zero = 0;
99 while (fill_ != 56)
100 update(&zero, 1);
101 std::array<std::uint8_t, 8> length{};
102 for (int i = 0; i < 8; ++i) {
103 length[i] = static_cast<std::uint8_t>(bits >> (56 - 8 * i));
104 }
105 update(length.data(), 8);
106 static const char* digits = "0123456789abcdef";
107 std::string out;
108 for (const std::uint32_t word: h_) {
109 for (int shift = 28; shift >= 0; shift -= 4) {
110 out.push_back(digits[(word >> shift) & 0xf]);
111 }
112 }
113 return out;
114 }
115
116private:
117 std::array<std::uint32_t, 8> h_ = {0x6a09e667, 0xbb67ae85, 0x3c6ef372,
118 0xa54ff53a, 0x510e527f, 0x9b05688c,
119 0x1f83d9ab, 0x5be0cd19};
120 std::array<std::uint8_t, 64> buffer_{};
121 std::size_t fill_ = 0;
122 std::uint64_t total_ = 0;
123};
124
125} // namespace
126
127std::string sha256Hex(const void* data, std::size_t size)
128{
129 Hasher hasher;
130 hasher.update(static_cast<const std::uint8_t*>(data), size);
131 return hasher.finish();
132}
133
134std::string sha256File(const std::string& path)
135{
136 std::ifstream in(path, std::ios::binary);
137 if (!in) return {};
138 Hasher hasher;
139 std::vector<std::uint8_t> chunk(1 << 20);
140 while (in) {
141 in.read(reinterpret_cast<char*>(chunk.data()),
142 static_cast<std::streamsize>(chunk.size()));
143 hasher.update(chunk.data(), static_cast<std::size_t>(in.gcount()));
144 }
145 return in.eof() ? hasher.finish() : std::string();
146}
147
148} // namespace gpr
@ x
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
std::string sha256Hex(const void *data, std::size_t size)
Lowercase hexadecimal SHA-256 digest of a byte range (FIPS 180-4).
Definition Sha256.cpp:127
std::string sha256File(const std::string &path)
Digest of a file's contents; empty when the file cannot be read.
Definition Sha256.cpp:134
Panel finish(Real left, Real right, const Value &k, const Value &g, const Value &absolute)