Skip to content

Commit fba7aba

Browse files
add md5 hashing (#764)
1 parent 4263dac commit fba7aba

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

hashing/md5/md5.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// md5.go
2+
// description: The MD5 hashing function as defined in RFC 1321.
3+
// author: Simon Waldherr
4+
// ref: https://datatracker.ietf.org/doc/html/rfc1321
5+
// see md5_test.go for testing
6+
7+
package md5
8+
9+
import (
10+
"encoding/binary"
11+
)
12+
13+
// Constants for MD5
14+
var (
15+
s = [64]uint32{
16+
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
17+
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
18+
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
19+
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
20+
}
21+
22+
K = [64]uint32{
23+
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
24+
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
25+
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
26+
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
27+
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
28+
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
29+
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
30+
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
31+
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
32+
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
33+
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
34+
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
35+
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
36+
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
37+
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
38+
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
39+
}
40+
)
41+
42+
// leftRotate rotates x left by n bits
43+
func leftRotate(x, n uint32) uint32 {
44+
return (x << n) | (x >> (32 - n))
45+
}
46+
47+
// pad pads the input message so that its length is congruent to 448 modulo 512
48+
func pad(message []byte) []byte {
49+
originalLength := len(message) * 8
50+
message = append(message, 0x80)
51+
for (len(message)*8)%512 != 448 {
52+
message = append(message, 0x00)
53+
}
54+
55+
lengthBytes := make([]byte, 8)
56+
binary.LittleEndian.PutUint64(lengthBytes, uint64(originalLength))
57+
message = append(message, lengthBytes...)
58+
59+
return message
60+
}
61+
62+
// Hash computes the MD5 hash of the input message
63+
func Hash(message []byte) [16]byte {
64+
message = pad(message)
65+
66+
// Initialize MD5 state variables
67+
a0, b0, c0, d0 := uint32(0x67452301), uint32(0xefcdab89), uint32(0x98badcfe), uint32(0x10325476)
68+
69+
// Process the message in successive 512-bit chunks
70+
for i := 0; i < len(message); i += 64 {
71+
chunk := message[i : i+64]
72+
var M [16]uint32
73+
for j := 0; j < 16; j++ {
74+
M[j] = binary.LittleEndian.Uint32(chunk[j*4 : (j+1)*4])
75+
}
76+
77+
// Initialize hash value for this chunk
78+
A, B, C, D := a0, b0, c0, d0
79+
80+
// Main loop
81+
for i := 0; i < 64; i++ {
82+
var F, g uint32
83+
if i < 16 {
84+
F = (B & C) | ((^B) & D)
85+
g = uint32(i)
86+
} else if i < 32 {
87+
F = (D & B) | ((^D) & C)
88+
g = uint32((5*i + 1) % 16)
89+
} else if i < 48 {
90+
F = B ^ C ^ D
91+
g = uint32((3*i + 5) % 16)
92+
} else {
93+
F = C ^ (B | (^D))
94+
g = uint32((7 * i) % 16)
95+
}
96+
F = F + A + K[i] + M[g]
97+
A = D
98+
D = C
99+
C = B
100+
B = B + leftRotate(F, s[i])
101+
}
102+
103+
// Add this chunk's hash to result so far
104+
a0 += A
105+
b0 += B
106+
c0 += C
107+
d0 += D
108+
}
109+
110+
// Produce the final hash value (digest)
111+
var digest [16]byte
112+
binary.LittleEndian.PutUint32(digest[0:4], a0)
113+
binary.LittleEndian.PutUint32(digest[4:8], b0)
114+
binary.LittleEndian.PutUint32(digest[8:12], c0)
115+
binary.LittleEndian.PutUint32(digest[12:16], d0)
116+
117+
return digest
118+
}

hashing/md5/md5_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// md5_test.go
2+
// description: Tests for the MD5 hashing function as defined in RFC 1321.
3+
// author: Simon Waldherr
4+
5+
package md5
6+
7+
import (
8+
"encoding/hex"
9+
"testing"
10+
)
11+
12+
// Helper function to convert hash output to hex string for comparison
13+
func toHexString(hash [16]byte) string {
14+
return hex.EncodeToString(hash[:])
15+
}
16+
17+
// Test vectors for MD5 (from RFC 1321 and other known sources)
18+
var tests = []struct {
19+
input string
20+
expected string
21+
}{
22+
{"", "d41d8cd98f00b204e9800998ecf8427e"},
23+
{"a", "0cc175b9c0f1b6a831c399e269772661"},
24+
{"abc", "900150983cd24fb0d6963f7d28e17f72"},
25+
{"message digest", "f96b697d7cb7938d525a2f31aaf161d0"},
26+
{"abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b"},
27+
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f"},
28+
{"12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a"},
29+
}
30+
31+
// TestHash verifies that the Hash function produces the correct MD5 hash values
32+
func TestHash(t *testing.T) {
33+
for _, tt := range tests {
34+
t.Run(tt.input, func(t *testing.T) {
35+
result := Hash([]byte(tt.input))
36+
resultHex := toHexString(result)
37+
if resultHex != tt.expected {
38+
t.Errorf("MD5(%q) = %s; want %s", tt.input, resultHex, tt.expected)
39+
}
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)