Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
hashing::sha256::Hash Class Reference

Contains hash array and functions to update it and convert it to a hexadecimal string. More...

Collaboration diagram for hashing::sha256::Hash:
[legend]

Public Member Functions

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.
 

Private Attributes

std::array< uint32_t, 8 > hash
 

Detailed Description

Contains hash array and functions to update it and convert it to a hexadecimal string.

Member Function Documentation

◆ to_string()

std::string hashing::sha256::Hash::to_string ( ) const

Convert the hash to a hexadecimal string.

Returns
std::string Final hash value
130 {
132 for (size_t i = 0; i < 8; ++i) {
133 ss << std::hex << std::setfill('0') << std::setw(8) << hash[i];
134 }
135 return ss.str();
136}
T hex(T... args)
T setfill(T... args)
T setw(T... args)
T str(T... args)
Here is the call graph for this function:

◆ update()

void hashing::sha256::Hash::update ( const std::array< uint32_t, 64 > & blocks)

Updates the hash array.

Parameters
blocksMessage schedule array
Returns
void
67 {
68 // Initialize array of round constants with first 32 bits of the fractional
69 // parts of the cube roots of the first 64 primes 2..311
70 const std::array<uint32_t, 64> round_constants = {
71 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
72 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
73 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
74 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
75 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147,
76 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
77 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B,
78 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
79 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A,
80 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
81 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};
82
83 // Initialize working variables
84 auto a = hash[0];
85 auto b = hash[1];
86 auto c = hash[2];
87 auto d = hash[3];
88 auto e = hash[4];
89 auto f = hash[5];
90 auto g = hash[6];
91 auto h = hash[7];
92
93 // Compression function main loop
94 for (size_t block_num = 0; block_num < 64; ++block_num) {
95 const auto s1 =
96 right_rotate(e, 6) ^ right_rotate(e, 11) ^ right_rotate(e, 25);
97 const auto ch = (e & f) ^ (~e & g);
98 const auto temp1 =
99 h + s1 + ch + round_constants[block_num] + blocks[block_num];
100 const auto s0 =
101 right_rotate(a, 2) ^ right_rotate(a, 13) ^ right_rotate(a, 22);
102 const auto maj = (a & b) ^ (a & c) ^ (b & c);
103 const auto temp2 = s0 + maj;
104
105 h = g;
106 g = f;
107 f = e;
108 e = d + temp1;
109 d = c;
110 c = b;
111 b = a;
112 a = temp1 + temp2;
113 }
114
115 // Update hash values
116 hash[0] += a;
117 hash[1] += b;
118 hash[2] += c;
119 hash[3] += d;
120 hash[4] += e;
121 hash[5] += f;
122 hash[6] += g;
123 hash[7] += h;
124}
double g(double x)
Another test function.
Definition composite_simpson_rule.cpp:115
double f(double x)
A function f(x) that will be used to test the method.
Definition composite_simpson_rule.cpp:113
int h(int key)
Definition hash_search.cpp:45
uint32_t right_rotate(uint32_t n, size_t rotate)
Rotates the bits of a 32-bit unsigned integer.
Definition sha256.cpp:58
Here is the call graph for this function:

Member Data Documentation

◆ hash

std::array<uint32_t, 8> hashing::sha256::Hash::hash
private
Initial value:
= {0x6A09E667, 0xBB67AE85, 0x3C6EF372,
0xA54FF53A, 0x510E527F, 0x9B05688C,
0x1F83D9AB, 0x5BE0CD19}
43 {0x6A09E667, 0xBB67AE85, 0x3C6EF372,
44 0xA54FF53A, 0x510E527F, 0x9B05688C,
45 0x1F83D9AB, 0x5BE0CD19};

The documentation for this class was generated from the following file: