mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Added description + some clean up
This commit is contained in:
parent
8d53d3c00d
commit
96d3980594
@ -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 <algorithm> // Used for std::copy
|
||||
#include <array> // To avoid c-style array (thanks clang-tidy)
|
||||
#include <array> // Used for std::array
|
||||
#include <cassert> // Used for assert
|
||||
#include <cstring> // Used for std::memcopy
|
||||
#include <iostream> // Used for IO operations
|
||||
#include <string> // Used for strings
|
||||
#include <vector> // To avoid c-style array of runtime size (thanks clang-tidy)
|
||||
|
||||
// TODO(runner): remove
|
||||
#include <bitset>
|
||||
#include <vector> // 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<const uint8_t*>(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);
|
||||
|
Loading…
Reference in New Issue
Block a user