Hash Family and How Hash Functions Work
Hash Family and How Hash Functions Work
Level 1
#include <vector>
#include <cstring>
SHA-512 follows a similar process to SHA-256 but works
class SHA512 {
with 64-bit words and produces a 512-bit hash.
public:
values derived from the first 64 bits of the fractional void update(const uint8_t* data, size_t length);
parts of the square roots of the first 8 prime numbers. void finalize(uint8_t hash[64]);
void reset();
2. Preprocessing: Pad the input so that its length is
private:
congruent to 896 modulo 1024. Append the original
void transform(const uint8_t* data);
length of the message as a 128-bit integer.
static const uint64_t k[80];
3. Processing: Divide the input into 1024-bit blocks. For uint64_t state[8];
uint64_t bit_length;
• Expand the block into 80 words of 64 bits each.
};
• Update the hash values with the results of the // Initial hash values
0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
4. Finalization: Concatenate the hash values to
0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
produce the final 512-bit hash.
0x510e527fade682d1, 0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
};
int main() {
SHA512 sha512;
uint8_t hash[64];
sha512.finalize(hash);
printf("%02x", hash[i]);
return 0;
}
#include <iostream>
#include <cstdint>
SHA-256 is a member of the SHA-2 family, producing a #include <cstring>
256-bit hash. Here's a detailed look at its processing class SHA256 {
steps: public:
values. These are derived from the first 32 bits of the void update(const uint8_t* data, size_t length);
fractional parts of the square roots of the first 8 prime void finalize(uint8_t hash[32]);
private:
2. Preprocessing: Pad the input so that its length is void transform(const uint8_t* data);
congruent to 448 modulo 512. Append the original static const uint32_t k[64];
length of the message as a 64-bit integer. uint32_t state[8];
3. Processing: Divide the input into 512-bit blocks. For std::vector<uint8_t> buffer;
};
• Expand the block into 64 words of 32 bits each. // Constants
• Initialize working variables to the current hash value. const uint32_t SHA256::k[64] = {
• Update the hash values with the results of the const uint32_t initial_state[8] = {
int main() {
SHA256 sha256;
uint8_t hash[32];
sha256.finalize(hash);
printf("%02x", hash[i]);
return 0;