TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Simple C++ implementation of the SHA-1 Hashing Algorithm More...
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | hashing |
Used for assert. | |
namespace | SHA |
Functions for the SHA-1 algorithm implementation. | |
Functions | |
uint32_t | hashing::sha1::leftRotate32bits (uint32_t n, std::size_t rotate) |
Rotates the bits of a 32-bit unsigned integer. | |
std::string | hashing::sha1::sig2hex (void *sig) |
Transforms the 160-bit SHA-1 signature into a 40 char hex string. | |
void * | hashing::sha1::hash_bs (const void *input_bs, uint64_t input_size) |
The SHA-1 algorithm itself, taking in a bytestring. | |
void * | hashing::sha1::hash (const std::string &message) |
Converts the string to bytestring and calls the main algorithm. | |
static void | test () |
Self-test implementations of well-known SHA-1 hashes. | |
static void | interactive () |
Puts user in a loop where inputs can be given and SHA-1 hash will be computed and printed. | |
int | main () |
Main function. | |
Simple C++ implementation of the SHA-1 Hashing Algorithm
SHA-1 is a cryptographic hash function that was developped by the NSA 1995. SHA-1 is not considered secure since around 2010.
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 for more precision on these operations
Definition in file sha1.cpp.
void * hashing::sha1::hash | ( | const std::string & | message | ) |
Converts the string to bytestring and calls the main algorithm.
message | Plain character message to hash |
Definition at line 211 of file sha1.cpp.
void * hashing::sha1::hash_bs | ( | const void * | input_bs, |
uint64_t | input_size ) |
The SHA-1 algorithm itself, taking in a bytestring.
input_bs | The bytestring to hash |
input_size | The size (in BYTES) of the input |
Definition at line 84 of file sha1.cpp.
|
static |
Puts user in a loop where inputs can be given and SHA-1 hash will be computed and printed.
Definition at line 275 of file sha1.cpp.
uint32_t hashing::sha1::leftRotate32bits | ( | uint32_t | n, |
std::size_t | rotate ) |
int main | ( | void | ) |
Main function.
Definition at line 300 of file sha1.cpp.
std::string hashing::sha1::sig2hex | ( | void * | sig | ) |
Transforms the 160-bit SHA-1 signature into a 40 char hex string.
sig | The SHA-1 signature (Expected 20 bytes) |
Definition at line 67 of file sha1.cpp.
|
static |
Self-test implementations of well-known SHA-1 hashes.
Definition at line 221 of file sha1.cpp.