mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: suggested changes
This commit is contained in:
parent
a1960b0fad
commit
53c0f6bcc1
@ -31,28 +31,51 @@ namespace hashing {
|
||||
* algorithm implementation
|
||||
*/
|
||||
namespace sha256 {
|
||||
/**
|
||||
* @brief Computes size of the padded input
|
||||
* @param input Input string
|
||||
* @return size_t Size of the padded input
|
||||
*/
|
||||
std::size_t compute_padded_size(const std::size_t input_size) {
|
||||
size_t padded_input_size = 0;
|
||||
if (input_size % 64 < 56) {
|
||||
padded_input_size = input_size + 64 - (input_size % 64);
|
||||
} else {
|
||||
padded_input_size = input_size + 128 - (input_size % 64);
|
||||
}
|
||||
return padded_input_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the byte at position byte_num in in_value
|
||||
* @param in_value Input value
|
||||
* @param byte_num Position of byte to be returned
|
||||
* @return uint8_t Byte at position byte_num
|
||||
*/
|
||||
uint8_t extract_byte(const std::size_t in_value, const std::size_t byte_num) {
|
||||
return ((in_value >> (byte_num * 8)) & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the character at pos after the input is padded
|
||||
* @param input Input string
|
||||
* @param pos Position of character to be returned
|
||||
* @param padded_input_size Size of the padded input
|
||||
* @return char Character at the index pos in the padded string
|
||||
*/
|
||||
char get_char(const std::string &input, std::size_t pos,
|
||||
size_t padded_input_size) {
|
||||
char get_char(const std::string &input, std::size_t pos) {
|
||||
size_t input_size = input.length();
|
||||
char ch = 0;
|
||||
size_t padded_input_size = compute_padded_size(input_size);
|
||||
if (pos < input_size) {
|
||||
ch = input[pos];
|
||||
} else if (pos == input_size) {
|
||||
ch = '\x80';
|
||||
} else if (pos < padded_input_size - 8) {
|
||||
ch = '\x00';
|
||||
} else {
|
||||
ch =
|
||||
static_cast<char>((input_size * 8 >> (56 - (pos % 56) * 8)) & 0xFF);
|
||||
return input[pos];
|
||||
}
|
||||
return ch;
|
||||
if (pos == input_size) {
|
||||
return '\x80';
|
||||
}
|
||||
if (pos < padded_input_size - 8) {
|
||||
return '\x00';
|
||||
}
|
||||
return static_cast<char>(
|
||||
extract_byte(input_size * 8, padded_input_size - pos - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,15 +115,10 @@ std::array<uint32_t, 8> compute_hash(const std::string &input) {
|
||||
0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
|
||||
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};
|
||||
|
||||
size_t input_size = input.length(); // Input size in bytes
|
||||
size_t input_size = input.length(); ///< Input size in bytes
|
||||
|
||||
// Calculate size of the padded input in bytes
|
||||
size_t padded_input_size = 0;
|
||||
if (input_size % 64 < 56) {
|
||||
padded_input_size = input_size + 64 - (input_size % 64);
|
||||
} else {
|
||||
padded_input_size = input_size + 128 - (input_size % 64);
|
||||
}
|
||||
size_t padded_input_size =
|
||||
compute_padded_size(input_size); ///< Padded input size in bytes
|
||||
|
||||
// Process message in successive 512-bit (64-byte) chunks
|
||||
for (size_t i = 0; i < padded_input_size; i += 64) {
|
||||
@ -108,17 +126,11 @@ std::array<uint32_t, 8> compute_hash(const std::string &input) {
|
||||
|
||||
// Copy chunk into first 16 words of the message schedule array
|
||||
for (size_t j = 0; j < 16; ++j) {
|
||||
blocks[j] = (static_cast<uint8_t>(
|
||||
get_char(input, i + j * 4, padded_input_size))
|
||||
<< 24) |
|
||||
(static_cast<uint8_t>(
|
||||
get_char(input, i + j * 4 + 1, padded_input_size))
|
||||
<< 16) |
|
||||
(static_cast<uint8_t>(
|
||||
get_char(input, i + j * 4 + 2, padded_input_size))
|
||||
<< 8) |
|
||||
static_cast<uint8_t>(
|
||||
get_char(input, i + j * 4 + 3, padded_input_size));
|
||||
blocks[j] =
|
||||
(static_cast<uint8_t>(get_char(input, i + j * 4)) << 24) |
|
||||
(static_cast<uint8_t>(get_char(input, i + j * 4 + 1)) << 16) |
|
||||
(static_cast<uint8_t>(get_char(input, i + j * 4 + 2)) << 8) |
|
||||
static_cast<uint8_t>(get_char(input, i + j * 4 + 3));
|
||||
}
|
||||
|
||||
for (size_t j = 16; j < 64; ++j) {
|
||||
@ -185,7 +197,7 @@ std::string hash_to_string(const std::array<uint32_t, 8> &hash) {
|
||||
std::stringstream ss;
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
for (size_t j = 0; j < 4; ++j) {
|
||||
uint32_t byte = (hash[i] >> (24 - j * 8)) & 0xFF;
|
||||
uint32_t byte = extract_byte(hash[i], 3 - j);
|
||||
ss << std::hex << std::setfill('0') << std::setw(2) << byte;
|
||||
}
|
||||
}
|
||||
@ -239,13 +251,5 @@ static void test() {
|
||||
*/
|
||||
int main() {
|
||||
test(); // Run self-test implementations
|
||||
|
||||
// Custom Input
|
||||
std::string input;
|
||||
std::cout << "Enter a message to be hashed: ";
|
||||
getline(std::cin, input);
|
||||
std::string hash = hashing::sha256::sha256(input);
|
||||
std::cout << "SHA-256 Hash: " << hash << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user