From 96d3980594709af0bfb844aa6ddf9ed52e116500 Mon Sep 17 00:00:00 2001 From: tGautot Date: Sun, 19 Sep 2021 22:56:45 +0200 Subject: [PATCH] Added description + some clean up --- hashing/sha1.cpp | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/hashing/sha1.cpp b/hashing/sha1.cpp index 70ffab504..53a5a7149 100644 --- a/hashing/sha1.cpp +++ b/hashing/sha1.cpp @@ -5,21 +5,37 @@ * Algorithm](https://en.wikipedia.org/wiki/SHA-1) * * @details - * TODO: Add + * [SHA-1](https://en.wikipedia.org/wiki/SHA-1) is a cryptographic hash function + * that was developped by the + * [NSA](https://en.wikipedia.org/wiki/National_Security_Agency) 1995. + * SHA-1 is not considered secure since around 2010. + * + * ### Algorithm + * The first step of the algorithm is to pad the message for its length to + * be a multiple of 64 (bytes). This is done by first adding 0x80 (10000000) + * and then only zeroes until the last 8 bytes must be filled, where then the + * 64 bit size of the input will be added + * + * Once this is done, the algo breaks down this padded message + * into 64 bytes chunks. Each chunk is used for one *round*, a round + * breaks the chunk into 16 blocks of 4 bytes. These 16 blocks are then extended + * to 80 blocks using XOR operations on existing blocks (see code for more + * details). The algorithm will then update its 160-bit state (here represented + * used 5 32-bits integer) using partial hashes computed using special functions + * on the blocks previously built. Please take a look at the [wikipedia + * article](https://en.wikipedia.org/wiki/SHA-1#SHA-1_pseudocode) for more + * precision on these operations * @note This is a simple implementation for a byte string but * some implmenetations can work on bytestream, messages of unknown length. */ #include // Used for std::copy -#include // To avoid c-style array (thanks clang-tidy) +#include // Used for std::array #include // Used for assert #include // Used for std::memcopy #include // Used for IO operations #include // Used for strings -#include // To avoid c-style array of runtime size (thanks clang-tidy) - -// TODO(runner): remove -#include +#include // Used for std::vector /** * @namespace hashing @@ -62,12 +78,12 @@ std::string sig2hex(void* sig) { * @brief The SHA-1 algorithm itself, taking in a bytestring * @param input_bs The bytestring to hash * @param input_size The size (in BYTES) of the input - * @return void* Pointer to the 128-bit signature + * @return void* Pointer to the 160-bit signature */ void* hash_bs(const void* input_bs, uint64_t input_size) { auto* input = static_cast(input_bs); - // Step 0: The initial 128-bit state + // Step 0: The initial 160-bit state uint32_t h0 = 0x67452301, a = 0; uint32_t h1 = 0xEFCDAB89, b = 0; uint32_t h2 = 0x98BADCFE, c = 0; @@ -171,9 +187,6 @@ void* hash_bs(const void* input_bs, uint64_t input_size) { h4 += e; } - std::bitset<32> x(h0); - std::cout << h0 << "\n" << x << std::endl; - // Build signature from state // Note, any type could be used for the signature // uint8_t was used to make the 20 bytes obvious @@ -261,7 +274,7 @@ static void test() { static void interactive() { while (true) { std::string input; - std::cout << "Enter a message to be hashed (only one line): " + std::cout << "Enter a message to be hashed (Ctrl-C to exit): " << std::endl; std::getline(std::cin, input); void* sig = hashing::sha1::hash(input);