Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@
* [Base32](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/base32.rs)
* [Base64](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/base64.rs)
* [Base85](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/base85.rs)
* [Blake2B](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/blake2b.rs)
* [Caesar](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/caesar.rs)
* [Chacha](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/chacha.rs)
* [Diffie-Hellman](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/diffie_hellman.rs)
* [Hashing Traits](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/hashing_traits.rs)
* [Hill Cipher](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/hill_cipher.rs)
* [Kernighan](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/kernighan.rs)
* [Morse Code](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/morse_code.rs)
Expand All @@ -55,9 +53,6 @@
* [ROT13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rot13.rs)
* [RSA Cipher](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rsa_cipher.rs)
* [Salsa](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/salsa.rs)
* [SHA-1](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha1.rs)
* [SHA-2](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha2.rs)
* [SHA-3](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha3.rs)
* [Tea](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/tea.rs)
* [Theoretical ROT13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/theoretical_rot13.rs)
* [Transposition](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/transposition.rs)
Expand Down Expand Up @@ -217,6 +212,13 @@
* [Minimum Coin Change](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/minimum_coin_changes.rs)
* [Smallest Range](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/smallest_range.rs)
* [Stable Matching](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/stable_matching.rs)
* Hashing
* [Blake2B](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/blake2b.rs)
* [Hashing Traits](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/hashing_traits.rs)
* [MD5](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/md5.rs)
* [SHA-1](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/sha1.rs)
* [SHA-2](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/sha2.rs)
* [SHA-3](https://github.com/TheAlgorithms/Rust/blob/master/src/hashing/sha3.rs)
* [Lib](https://github.com/TheAlgorithms/Rust/blob/master/src/lib.rs)
* Machine Learning
* [Cholesky](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/cholesky.rs)
Expand Down
10 changes: 0 additions & 10 deletions src/ciphers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ mod base16;
mod base32;
mod base64;
mod base85;
mod blake2b;
mod caesar;
mod chacha;
mod diffie_hellman;
mod hashing_traits;
mod hill_cipher;
mod kernighan;
mod morse_code;
Expand All @@ -19,9 +17,6 @@ mod rail_fence;
mod rot13;
mod rsa_cipher;
mod salsa;
mod sha1;
mod sha2;
mod sha3;
mod tea;
mod theoretical_rot13;
mod transposition;
Expand All @@ -38,11 +33,9 @@ pub use self::base16::{base16_decode, base16_encode};
pub use self::base32::{base32_decode, base32_encode};
pub use self::base64::{base64_decode, base64_encode};
pub use self::base85::{base85_decode, base85_encode};
pub use self::blake2b::blake2b;
pub use self::caesar::caesar;
pub use self::chacha::chacha20;
pub use self::diffie_hellman::DiffieHellman;
pub use self::hashing_traits::{Hasher, HMAC};
pub use self::hill_cipher::HillCipher;
pub use self::kernighan::kernighan;
pub use self::morse_code::{decode, encode};
Expand All @@ -53,9 +46,6 @@ pub use self::rsa_cipher::{
decrypt, decrypt_text, encrypt, encrypt_text, generate_keypair, PrivateKey, PublicKey,
};
pub use self::salsa::salsa20;
pub use self::sha1::sha1;
pub use self::sha2::{sha224, sha256, sha384, sha512, sha512_224, sha512_256};
pub use self::sha3::{sha3_224, sha3_256, sha3_384, sha3_512};
pub use self::tea::{tea_decrypt, tea_encrypt};
pub use self::theoretical_rot13::theoretical_rot13;
pub use self::transposition::transposition;
Expand Down
File renamed without changes.
File renamed without changes.
173 changes: 173 additions & 0 deletions src/hashing/md5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use std::fmt::Write;

// MD5 hash function implementation
// Reference: https://www.ietf.org/rfc/rfc1321.txt
//
// MD5 produces a 128-bit (16-byte) hash value.
// Note: MD5 is cryptographically broken and should NOT be used for security
// purposes. It remains useful for checksums and non-security applications.

/// Per-round shift amounts (RFC 1321, §3.4)
const S: [u32; 64] = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, // Round 1
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, // Round 2
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, // Round 3
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, // Round 4
];

/// Precomputed table of abs(sin(i+1)) * 2^32 (RFC 1321, §3.4)
const K: [u32; 64] = [
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
];

/// Initial hash state (RFC 1321, §3.3) — "magic" little-endian constants
const INIT_STATE: [u32; 4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];

/// Computes the MD5 hash of the given byte slice.
/// Returns a 16-byte array representing the 128-bit digest.
pub fn md5(input: &[u8]) -> [u8; 16] {
let mut state = INIT_STATE;

// --- Pre-processing: padding ---
// Append bit '1' (0x80 byte), then zeros, then 64-bit little-endian
// message length in bits, so total length ≡ 448 (mod 512) bits.
let bit_len = (input.len() as u64).wrapping_mul(8);
let mut msg = input.to_vec();
msg.push(0x80);
while msg.len() % 64 != 56 {
msg.push(0x00);
}
msg.extend_from_slice(&bit_len.to_le_bytes());

// --- Processing: 512-bit (64-byte) chunks ---
for chunk in msg.chunks_exact(64) {
// Break chunk into 16 little-endian 32-bit words
let mut m = [0u32; 16];
for (i, word) in m.iter_mut().enumerate() {
let offset = i * 4;
*word = u32::from_le_bytes(chunk[offset..offset + 4].try_into().unwrap());
}

let [mut a, mut b, mut c, mut d] = state;

for i in 0..64u32 {
let (f, g) = match i {
0..=15 => ((b & c) | (!b & d), i),
16..=31 => ((d & b) | (!d & c), (5 * i + 1) % 16),
32..=47 => (b ^ c ^ d, (3 * i + 5) % 16),
_ => (c ^ (b | !d), (7 * i) % 16),
};

let temp = d;
d = c;
c = b;
b = b.wrapping_add(
(a.wrapping_add(f)
.wrapping_add(K[i as usize])
.wrapping_add(m[g as usize]))
.rotate_left(S[i as usize]),
);
a = temp;
}

state[0] = state[0].wrapping_add(a);
state[1] = state[1].wrapping_add(b);
state[2] = state[2].wrapping_add(c);
state[3] = state[3].wrapping_add(d);
}

// --- Produce final digest (little-endian word order) ---
let mut digest = [0u8; 16];
for (i, &word) in state.iter().enumerate() {
digest[i * 4..i * 4 + 4].copy_from_slice(&word.to_le_bytes());
}
digest
}

/// Convenience helper: returns the MD5 digest as a lowercase hex string.
pub fn md5_hex(input: &[u8]) -> String {
md5(input)
.iter()
.fold(String::with_capacity(32), |mut s, b| {
write!(s, "{b:02x}").unwrap();
s
})
}

#[cfg(test)]
mod tests {
use super::*;

// All expected values from the RFC 1321 test suite and NIST vectors.

#[test]
fn test_empty_string() {
assert_eq!(md5_hex(b""), "d41d8cd98f00b204e9800998ecf8427e");
}

#[test]
fn test_abc() {
assert_eq!(md5_hex(b"abc"), "900150983cd24fb0d6963f7d28e17f72");
}

#[test]
fn test_rfc_message() {
assert_eq!(
md5_hex(b"message digest"),
"f96b697d7cb7938d525a2f31aaf161d0"
);
}

#[test]
fn test_alphabet() {
assert_eq!(
md5_hex(b"abcdefghijklmnopqrstuvwxyz"),
"c3fcd3d76192e4007dfb496cca67e13b"
);
}

#[test]
fn test_alphanumeric() {
assert_eq!(
md5_hex(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"),
"d174ab98d277d9f5a5611c2c9f419d9f"
);
}

#[test]
fn test_digits_repeated() {
assert_eq!(
md5_hex(
b"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
),
"57edf4a22be3c955ac49da2e2107b67a"
);
}

#[test]
fn test_single_char() {
assert_eq!(md5_hex(b"a"), "0cc175b9c0f1b6a831c399e269772661");
}

#[test]
fn test_returns_16_bytes() {
assert_eq!(md5(b"hello").len(), 16);
}

#[test]
fn test_deterministic() {
assert_eq!(md5(b"rust"), md5(b"rust"));
}

#[test]
fn test_different_inputs_differ() {
assert_ne!(md5(b"foo"), md5(b"bar"));
}
}
13 changes: 13 additions & 0 deletions src/hashing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod blake2b;
mod hashing_traits;
mod md5;
mod sha1;
mod sha2;
mod sha3;

pub use self::blake2b::blake2b;
pub use self::hashing_traits::{Hasher, HMAC};
pub use self::md5::{md5, md5_hex};
pub use self::sha1::sha1;
pub use self::sha2::{sha224, sha256, sha384, sha512, sha512_224, sha512_256};
pub use self::sha3::{sha3_224, sha3_256, sha3_384, sha3_512};
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod general;
pub mod geometry;
pub mod graph;
pub mod greedy;
pub mod hashing;
pub mod machine_learning;
pub mod math;
pub mod navigation;
Expand Down
Loading