Skip to content

Commit 53c0f6b

Browse files
author
Md. Anisul Haque
committed
fix: suggested changes
1 parent a1960b0 commit 53c0f6b

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

hashing/sha256.cpp

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,51 @@ namespace hashing {
3131
* algorithm implementation
3232
*/
3333
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+
3459
/**
3560
* @brief Returns the character at pos after the input is padded
3661
* @param input Input string
3762
* @param pos Position of character to be returned
38-
* @param padded_input_size Size of the padded input
3963
* @return char Character at the index pos in the padded string
4064
*/
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) {
4366
size_t input_size = input.length();
44-
char ch = 0;
67+
size_t padded_input_size = compute_padded_size(input_size);
4568
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';
5473
}
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));
5679
}
5780

5881
/**
@@ -92,33 +115,22 @@ std::array<uint32_t, 8> compute_hash(const std::string &input) {
92115
0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
93116
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};
94117

95-
size_t input_size = input.length(); // Input size in bytes
118+
size_t input_size = input.length(); ///< Input size in bytes
96119

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
104122

105123
// Process message in successive 512-bit (64-byte) chunks
106124
for (size_t i = 0; i < padded_input_size; i += 64) {
107125
std::array<uint32_t, 64> blocks{};
108126

109127
// Copy chunk into first 16 words of the message schedule array
110128
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));
122134
}
123135

124136
for (size_t j = 16; j < 64; ++j) {
@@ -185,7 +197,7 @@ std::string hash_to_string(const std::array<uint32_t, 8> &hash) {
185197
std::stringstream ss;
186198
for (size_t i = 0; i < 8; ++i) {
187199
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);
189201
ss << std::hex << std::setfill('0') << std::setw(2) << byte;
190202
}
191203
}
@@ -239,13 +251,5 @@ static void test() {
239251
*/
240252
int main() {
241253
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-
250254
return 0;
251255
}

0 commit comments

Comments
 (0)