@@ -31,28 +31,51 @@ namespace hashing {
31
31
* algorithm implementation
32
32
*/
33
33
namespace sha256 {
34
+ /* *
35
+ * @brief Computes size of the padded input
36
+ * @param input Input string
37
+ * @return size_t Size of the padded input
38
+ */
39
+ std::size_t compute_padded_size (const std::size_t input_size) {
40
+ size_t padded_input_size = 0 ;
41
+ if (input_size % 64 < 56 ) {
42
+ padded_input_size = input_size + 64 - (input_size % 64 );
43
+ } else {
44
+ padded_input_size = input_size + 128 - (input_size % 64 );
45
+ }
46
+ return padded_input_size;
47
+ }
48
+
49
+ /* *
50
+ * @brief Returns the byte at position byte_num in in_value
51
+ * @param in_value Input value
52
+ * @param byte_num Position of byte to be returned
53
+ * @return uint8_t Byte at position byte_num
54
+ */
55
+ uint8_t extract_byte (const std::size_t in_value, const std::size_t byte_num) {
56
+ return ((in_value >> (byte_num * 8 )) & 0xFF );
57
+ }
58
+
34
59
/* *
35
60
* @brief Returns the character at pos after the input is padded
36
61
* @param input Input string
37
62
* @param pos Position of character to be returned
38
- * @param padded_input_size Size of the padded input
39
63
* @return char Character at the index pos in the padded string
40
64
*/
41
- char get_char (const std::string &input, std::size_t pos,
42
- size_t padded_input_size) {
65
+ char get_char (const std::string &input, std::size_t pos) {
43
66
size_t input_size = input.length ();
44
- char ch = 0 ;
67
+ size_t padded_input_size = compute_padded_size (input_size) ;
45
68
if (pos < input_size) {
46
- ch = input[pos];
47
- } else if (pos == input_size) {
48
- ch = ' \x80 ' ;
49
- } else if (pos < padded_input_size - 8 ) {
50
- ch = ' \x00 ' ;
51
- } else {
52
- ch =
53
- static_cast <char >((input_size * 8 >> (56 - (pos % 56 ) * 8 )) & 0xFF );
69
+ return input[pos];
70
+ }
71
+ if (pos == input_size) {
72
+ return ' \x80 ' ;
54
73
}
55
- return ch;
74
+ if (pos < padded_input_size - 8 ) {
75
+ return ' \x00 ' ;
76
+ }
77
+ return static_cast <char >(
78
+ extract_byte (input_size * 8 , padded_input_size - pos - 1 ));
56
79
}
57
80
58
81
/* *
@@ -92,33 +115,22 @@ std::array<uint32_t, 8> compute_hash(const std::string &input) {
92
115
0x5B9CCA4F , 0x682E6FF3 , 0x748F82EE , 0x78A5636F , 0x84C87814 , 0x8CC70208 ,
93
116
0x90BEFFFA , 0xA4506CEB , 0xBEF9A3F7 , 0xC67178F2 };
94
117
95
- size_t input_size = input.length (); // Input size in bytes
118
+ size_t input_size = input.length (); // /< Input size in bytes
96
119
97
- // Calculate size of the padded input in bytes
98
- size_t padded_input_size = 0 ;
99
- if (input_size % 64 < 56 ) {
100
- padded_input_size = input_size + 64 - (input_size % 64 );
101
- } else {
102
- padded_input_size = input_size + 128 - (input_size % 64 );
103
- }
120
+ size_t padded_input_size =
121
+ compute_padded_size (input_size); // /< Padded input size in bytes
104
122
105
123
// Process message in successive 512-bit (64-byte) chunks
106
124
for (size_t i = 0 ; i < padded_input_size; i += 64 ) {
107
125
std::array<uint32_t , 64 > blocks{};
108
126
109
127
// Copy chunk into first 16 words of the message schedule array
110
128
for (size_t j = 0 ; j < 16 ; ++j) {
111
- blocks[j] = (static_cast <uint8_t >(
112
- get_char (input, i + j * 4 , padded_input_size))
113
- << 24 ) |
114
- (static_cast <uint8_t >(
115
- get_char (input, i + j * 4 + 1 , padded_input_size))
116
- << 16 ) |
117
- (static_cast <uint8_t >(
118
- get_char (input, i + j * 4 + 2 , padded_input_size))
119
- << 8 ) |
120
- static_cast <uint8_t >(
121
- get_char (input, i + j * 4 + 3 , padded_input_size));
129
+ blocks[j] =
130
+ (static_cast <uint8_t >(get_char (input, i + j * 4 )) << 24 ) |
131
+ (static_cast <uint8_t >(get_char (input, i + j * 4 + 1 )) << 16 ) |
132
+ (static_cast <uint8_t >(get_char (input, i + j * 4 + 2 )) << 8 ) |
133
+ static_cast <uint8_t >(get_char (input, i + j * 4 + 3 ));
122
134
}
123
135
124
136
for (size_t j = 16 ; j < 64 ; ++j) {
@@ -185,7 +197,7 @@ std::string hash_to_string(const std::array<uint32_t, 8> &hash) {
185
197
std::stringstream ss;
186
198
for (size_t i = 0 ; i < 8 ; ++i) {
187
199
for (size_t j = 0 ; j < 4 ; ++j) {
188
- uint32_t byte = (hash[i] >> ( 24 - j * 8 )) & 0xFF ;
200
+ uint32_t byte = extract_byte (hash[i], 3 - j) ;
189
201
ss << std::hex << std::setfill (' 0' ) << std::setw (2 ) << byte;
190
202
}
191
203
}
@@ -239,13 +251,5 @@ static void test() {
239
251
*/
240
252
int main () {
241
253
test (); // Run self-test implementations
242
-
243
- // Custom Input
244
- std::string input;
245
- std::cout << " Enter a message to be hashed: " ;
246
- getline (std::cin, input);
247
- std::string hash = hashing::sha256::sha256 (input);
248
- std::cout << " SHA-256 Hash: " << hash << std::endl;
249
-
250
254
return 0 ;
251
255
}
0 commit comments