Skip to content

Commit de9c0b9

Browse files
author
Md. Anisul Haque
committed
fix: suggested changes
1 parent 9cc7150 commit de9c0b9

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

hashing/sha256.cpp

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
* used for authenticating software packages and secure password hashing.
1313
*/
1414

15+
#include <array> /// For std::array
1516
#include <cassert> /// For assert
1617
#include <cstdint> /// For uint8_t, uint32_t and uint64_t data types
18+
#include <iomanip> /// For std::setfill and std::setw
1719
#include <iostream> /// For IO operations
20+
#include <sstream> /// For std::stringstream
1821

1922
/**
2023
* @namespace hashing
@@ -27,13 +30,37 @@ namespace hashing {
2730
* algorithm implementation
2831
*/
2932
namespace sha256 {
33+
/**
34+
* @brief Pads the input string to be a multiple of 512-bits
35+
* @param input Input string
36+
* @return std::string The padded input string
37+
*/
38+
std::string prepare_input(std::string input) {
39+
// Pre-processing
40+
std::string padded_input = input;
41+
uint64_t input_size = input.length() * 8; // Message length in bits
42+
43+
// Append a single '1' bit
44+
padded_input += '\x80';
45+
46+
// Pad with zeros
47+
while ((padded_input.length() * 8) % 512 != 448) padded_input += '\x00';
48+
49+
// Append length of the original input string as a 64-bit big-endian integer
50+
for (uint32_t i = 64; i != 0; i -= 8) {
51+
padded_input += static_cast<char>((input_size >> (i - 8)) & 0xFF);
52+
}
53+
54+
return padded_input;
55+
}
56+
3057
/**
3158
* @brief Rotates the bits of a 32-bit unsigned integer
3259
* @param n Integer to rotate
3360
* @param rotate Number of bits to rotate
3461
* @return uint32_t The rotated integer
3562
*/
36-
uint32_t rightRotate(uint32_t n, size_t rotate) {
63+
uint32_t right_rotate(uint32_t n, size_t rotate) {
3764
return (n >> rotate) | (n << (32 - rotate));
3865
}
3966

@@ -45,12 +72,13 @@ uint32_t rightRotate(uint32_t n, size_t rotate) {
4572
std::string sha256(const std::string &input) {
4673
// Initialize array of hash values with first 32 bits of the fractional
4774
// parts of the square roots of the first 8 primes 2..19
48-
uint32_t hash[8] = {0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
49-
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19};
75+
std::array<uint32_t, 8> hash = {0x6A09E667, 0xBB67AE85, 0x3C6EF372,
76+
0xA54FF53A, 0x510E527F, 0x9B05688C,
77+
0x1F83D9AB, 0x5BE0CD19};
5078

5179
// Initialize array of round constants with first 32 bits of the fractional
5280
// parts of the cube roots of the first 64 primes 2..311
53-
const uint32_t k[64] = {
81+
const std::array<uint32_t, 64> k = {
5482
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
5583
0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
5684
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
@@ -63,24 +91,11 @@ std::string sha256(const std::string &input) {
6391
0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
6492
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};
6593

66-
// Pre-processing
67-
std::string padded_input = input;
68-
uint64_t input_size = input.length() * 8; // Message length in bits
69-
70-
// Append a single '1' bit
71-
padded_input += '\x80';
72-
73-
// Pad with zeros
74-
while ((padded_input.length() * 8) % 512 != 448) padded_input += '\x00';
75-
76-
// Append length of the original input string as a 64-bit big-endian integer
77-
for (uint32_t i = 64; i != 0; i -= 8) {
78-
padded_input += static_cast<char>((input_size >> (i - 8)) & 0xFF);
79-
}
94+
const std::string padded_input = prepare_input(input);
8095

8196
// Process message in successive 512-bit (64-byte) chunks
8297
for (size_t i = 0; i < padded_input.length(); i += 64) {
83-
uint32_t blocks[64];
98+
std::array<uint32_t, 64> blocks;
8499

85100
// Copy chunk into first 16 words of the message schedule array
86101
for (size_t j = 0; j < 16; ++j) {
@@ -92,11 +107,11 @@ std::string sha256(const std::string &input) {
92107
}
93108

94109
for (size_t j = 16; j < 64; ++j) {
95-
uint32_t s0 = rightRotate(blocks[j - 15], 7) ^
96-
rightRotate(blocks[j - 15], 18) ^
110+
uint32_t s0 = right_rotate(blocks[j - 15], 7) ^
111+
right_rotate(blocks[j - 15], 18) ^
97112
(blocks[j - 15] >> 3);
98-
uint32_t s1 = rightRotate(blocks[j - 2], 17) ^
99-
rightRotate(blocks[j - 2], 19) ^
113+
uint32_t s1 = right_rotate(blocks[j - 2], 17) ^
114+
right_rotate(blocks[j - 2], 19) ^
100115
(blocks[j - 2] >> 10);
101116
blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1;
102117
}
@@ -114,11 +129,11 @@ std::string sha256(const std::string &input) {
114129
// Compression function main loop
115130
for (size_t j = 0; j < 64; ++j) {
116131
uint32_t s1 =
117-
rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25);
132+
right_rotate(e, 6) ^ right_rotate(e, 11) ^ right_rotate(e, 25);
118133
uint32_t ch = (e & f) ^ (~e & g);
119134
uint32_t temp1 = h + s1 + ch + k[j] + blocks[j];
120135
uint32_t s0 =
121-
rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22);
136+
right_rotate(a, 2) ^ right_rotate(a, 13) ^ right_rotate(a, 22);
122137
uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
123138
uint32_t temp2 = s0 + maj;
124139

@@ -146,11 +161,11 @@ std::string sha256(const std::string &input) {
146161
// Convert the hash to a hexadecimal string
147162
std::string result;
148163
for (size_t i = 0; i < 8; ++i) {
149-
for (int j = 0; j < 4; ++j) {
150-
uint8_t byte = (hash[i] >> (24 - j * 8)) & 0xFF;
151-
char buf[3];
152-
sprintf(buf, "%02x", byte);
153-
result += buf;
164+
for (size_t j = 0; j < 4; ++j) {
165+
uint32_t byte = (hash[i] >> (24 - j * 8)) & 0xFF;
166+
std::stringstream ss;
167+
ss << std::hex << std::setfill('0') << std::setw(2) << byte;
168+
result += ss.str();
154169
}
155170
}
156171

0 commit comments

Comments
 (0)