Implements the Message-Digest algorithm 5.

View versions (2)

Interface

Overview

This module implements the MD5 (Message-Digest algorithm 5) algorithm, which is a 128-bit valued hash function designed by Ron Rivest in 1991. Its main application is to check the integrity of data, but it can also be used in conjunction with cryptosystems like RSA.

The idea of the algorithm is to encode the input message using four 32-bit registers and by going through a series of bitwise operations, based on the following four functions:

$$F(x, y, z) := (x \wedge y) \vee (\neg x \wedge z)$$
(1)
$$G(x, y, z) := (x \wedge z) \vee (y \wedge \neg z),$$
(2)
$$H(x, y, z) := x \oplus y \oplus z,$$
(3)
$$I(x, y, z) := y \oplus (x \vee \neg z).$$
(4)

The algorithm also uses a number of 64 constants $c_0$, $c_1$, ..., $c_{63}$ defined through:

$$c_i := \left\lfloor 2^{32} \left| \sin(i + 1) \right| \right\rfloor, \qquad 0 \leq i \leq 63,$$
(5)

where $\lfloor \cdot \rfloor$ denotes the floor function.

The following example displays the MD5 code for the string "somewhere over the rainbow".

Example 1

#include <iostream>
#include <codecogs/computing/security/md5.h>

int main()
{
  std::string data = "somewhere over the rainbow";

  std::cout << "Input: " << data << std::endl;
  std::cout << "  MD5: " << Computing::Security::md5(data) << std::endl;

  return 0;
}

Output

Input: somewhere over the rainbow
  MD5: fe6acc5bea48d66ac6baa60ff73f6d62

References

GPL Licence — free for non commercial use. See Licence details.

FUNCTION

md5AddRotate

Parameters

value
the accumulated value to left-rotate
B
the register value to add after rotating
shiftCount
the number of bits to left-rotate value by

FUNCTION

md5F

Parameters

A
the first working register
B
the second working register
C
the third working register
D
the fourth working register
blockItem
the current 32-bit chunk of the message block
shiftCount
the number of bits to left-rotate by
sineValue
this round's precomputed constant, based on the sine function

FUNCTION

md5G

Parameters

A
the first working register
B
the second working register
C
the third working register
D
the fourth working register
blockItem
the current 32-bit chunk of the message block
shiftCount
the number of bits to left-rotate by
sineValue
this round's precomputed constant, based on the sine function

FUNCTION

md5H

Parameters

A
the first working register
B
the second working register
C
the third working register
D
the fourth working register
blockItem
the current 32-bit chunk of the message block
shiftCount
the number of bits to left-rotate by
sineValue
this round's precomputed constant, based on the sine function

FUNCTION

md5I

Parameters

A
the first working register
B
the second working register
C
the third working register
D
the fourth working register
blockItem
the current 32-bit chunk of the message block
shiftCount
the number of bits to left-rotate by
sineValue
this round's precomputed constant, based on the sine function

FUNCTION

md5

This function generates the unique MD5 code corresponding to the given string, in the form of a 32 digits hexadecimal number.

For example, to obtain the MD5 code of the string "Hello World", you would write something similar to:

std::string md5Code = Computing::Security::md5("Hello World");

Parameters

str
the string to be hashed

Returns

the MD5 code corresponding to str, as a 32 digits hexadecimal number stored as a string

Interactive Calculator

str
Result