Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Simple C++ implementation of the MD5 Hashing Algorithm More...
#include <algorithm>
#include <array>
#include <cassert>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
Namespaces | |
namespace | hashing |
Hashing algorithms. | |
namespace | MD5 |
Functions for the MD5 algorithm implementation. | |
Functions | |
uint32_t | hashing::md5::leftRotate32bits (uint32_t n, std::size_t rotate) |
Rotates the bits of a 32-bit unsigned integer. | |
bool | hashing::md5::isBigEndian () |
Checks whether integers are stored as big endian or not. | |
uint32_t | hashing::md5::toLittleEndian32 (uint32_t n) |
Sets 32-bit integer to little-endian if needed. | |
uint64_t | hashing::md5::toLittleEndian64 (uint64_t n) |
Sets 64-bit integer to little-endian if needed. | |
std::string | hashing::md5::sig2hex (void *sig) |
Transforms the 128-bit MD5 signature into a 32 char hex string. | |
void * | hashing::md5::hash_bs (const void *input_bs, uint64_t input_size) |
The MD5 algorithm itself, taking in a bytestring. | |
void * | hashing::md5::hash (const std::string &message) |
Converts the string to bytestring and calls the main algorithm. | |
static void | test () |
Self-test implementations of well-known MD5 hashes. | |
static void | interactive () |
Puts user in a loop where inputs can be given and MD5 hash will be computed and printed. | |
int | main () |
Main function. | |
Simple C++ implementation of the MD5 Hashing Algorithm
The MD5 Algorithm is a hashing algorithm which was designed in 1991 by Ronal Rivest.
MD5 is one of the most used hashing algorithm there is. Some of its use cases are:
However MD5 has be know to be cryptographically weak for quite some time, yet it is still widely used. This weakness was exploited by the Flame Malware in 2012
First of all, all values are expected to be in [little endian] (https://en.wikipedia.org/wiki/Endianness). This is especially important when using part of the bytestring as an integer.
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. During these rounds the algorithm will update its 128 bit state (represented by 4 ints: A,B,C,D) For more precisions on these operations please see the Wikipedia aritcle. The signature given by MD5 is its 128 bit state once all rounds are done.
void * hashing::md5::hash | ( | const std::string & | message | ) |
Converts the string to bytestring and calls the main algorithm.
message | Plain character message to hash |
void * hashing::md5::hash_bs | ( | const void * | input_bs, |
uint64_t | input_size ) |
The MD5 algorithm itself, taking in a bytestring.
input_bs | The bytestring to hash |
input_size | The size (in BYTES) of the input |
Values of K are pseudo-random and used to "salt" each round The values can be obtained by the following python code
|
static |
Puts user in a loop where inputs can be given and MD5 hash will be computed and printed.
bool hashing::md5::isBigEndian | ( | ) |
Checks whether integers are stored as big endian or not.
uint32_t hashing::md5::leftRotate32bits | ( | uint32_t | n, |
std::size_t | rotate ) |
Rotates the bits of a 32-bit unsigned integer.
n | Integer to rotate |
rotate | How many bits for the rotation |
int main | ( | void | ) |
Main function.
std::string hashing::md5::sig2hex | ( | void * | sig | ) |
Transforms the 128-bit MD5 signature into a 32 char hex string.
sig | The MD5 signature (Expected 16 bytes) |
|
static |
Self-test implementations of well-known MD5 hashes.
uint32_t hashing::md5::toLittleEndian32 | ( | uint32_t | n | ) |
Sets 32-bit integer to little-endian if needed.
n | Number to set to little-endian (uint32_t) |
uint64_t hashing::md5::toLittleEndian64 | ( | uint64_t | n | ) |
Sets 64-bit integer to little-endian if needed.
n | Number to set to little-endian (uint64_t) |