std::array<uint32_t, 8> hash = {0x6A09E667, 0xBB67AE85, 0x3C6EF372,
440xA54FF53A, 0x510E527F, 0x9B05688C,
450x1F83D9AB, 0x5BE0CD19};
48 void update(
conststd::array<uint32_t, 64> &blocks);
59 return(n >> rotate) | (n << (32 - rotate));
70 conststd::array<uint32_t, 64> round_constants = {
710x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
720x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
730x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
740x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
750x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147,
760x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
770x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B,
780xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
790x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A,
800x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
810x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};
94 for(
size_tblock_num = 0; block_num < 64; ++block_num) {
97 const autoch = (e & f) ^ (~e & g);
99 h+ s1 + ch + round_constants[block_num] + blocks[block_num];
102 const automaj = (a & b) ^ (a & c) ^ (b & c);
103 const autotemp2 = s0 + maj;
131std::stringstream ss;
132 for(
size_ti = 0; i < 8; ++i) {
133ss << std::hex << std::setfill(
'0') << std::setw(8) << hash[i];
144 if(input_size % 64 < 56) {
145 returninput_size + 64 - (input_size % 64);
147 returninput_size + 128 - (input_size % 64);
156template<
typenameT>
158 if(
sizeof(in_value) <= byte_num) {
159 throwstd::out_of_range(
"Byte at index byte_num does not exist");
161 return(in_value >> (byte_num * 8)) & 0xFF;
170char get_char(
conststd::string &input, std::size_t pos) {
171 const autoinput_size = input.length();
172 if(pos < input_size) {
175 if(pos == input_size) {
179 if(pos < padded_input_size - 8) {
182 if(padded_input_size <= pos) {
183 throwstd::out_of_range(
"pos is out of range");
185 return static_cast<char>(
196 const size_tbyte_num) {
197std::array<uint32_t, 64> blocks{};
200 for(
size_tblock_num = 0; block_num < 16; ++block_num) {
202(
static_cast<uint8_t
>(
get_char(input, byte_num + block_num * 4))
204(
static_cast<uint8_t
>(
get_char(input, byte_num + block_num * 4 + 1))
206(
static_cast<uint8_t
>(
get_char(input, byte_num + block_num * 4 + 2))
208 static_cast<uint8_t
>(
get_char(input, byte_num + block_num * 4 + 3));
213 for(
size_tblock_num = 16; block_num < 64; ++block_num) {
214 const autos0 =
right_rotate(blocks[block_num - 15], 7) ^
216(blocks[block_num - 15] >> 3);
217 const autos1 =
right_rotate(blocks[block_num - 2], 17) ^
219(blocks[block_num - 2] >> 10);
221blocks[block_num - 16] + s0 + blocks[block_num - 7] + s1;
232std::string
sha256(
conststd::string &input) {
239 return h.to_string();
254static voidtest_extract_byte() {
257 boolexception =
false;
260}
catch(
conststd::out_of_range &) {
266static voidtest_get_char() {
271 boolexception =
false;
274}
catch(
conststd::out_of_range &) {
280static voidtest_right_rotate() {
286static voidtest_sha256() {
288 conststd::string input;
289 conststd::string expected_hash;
290TestCase(std::string input, std::string expected_hash)
291: input(std::move(input)),
292expected_hash(std::move(expected_hash)) {}
294 conststd::vector<TestCase> test_cases{
297 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
300 "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"),
303 "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"),
305 "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9" 307 for(
const auto&tc : test_cases) {
319std::cout <<
"All tests have successfully passed!\n";
Contains hash array and functions to update it and convert it to a hexadecimal string.
void update(const std::array< uint32_t, 64 > &blocks)
Updates the hash array.
std::string to_string() const
Convert the hash to a hexadecimal string.
static void test()
Self-test implementations.
std::size_t compute_padded_size(const std::size_t input_size)
Computes size of the padded input.
std::array< uint32_t, 64 > create_message_schedule_array(const std::string &input, const size_t byte_num)
Creates the message schedule array.
std::string sha256(const std::string &input)
Computes the final hash value.
char get_char(const std::string &input, std::size_t pos)
Returns the character at pos after the input is padded.
uint32_t right_rotate(uint32_t n, size_t rotate)
Rotates the bits of a 32-bit unsigned integer.
int main()
Main function.
static void test_compute_padded_size()
Self-test implementations.
uint8_t extract_byte(const T in_value, const std::size_t byte_num)
Returns the byte at position byte_num in in_value.
represents single example inputs and expected output of the function longest_common_string_length
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4