Skip to content

feat: Added MD5 hashing algorithm #1519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 33 additions & 9 deletions Hashes/MD5.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ function u8ToU32(u8Array) {
return uint32Array
}

/**
* Converts Uint32Array to Uint8Array
*
* @param {Uint32Array} u32Array Uint32Array to convert
* @returns {Uint8Array} - Required Uint8Array
*/
function u32ToU8(u32Array) {
const uint8Array = new Uint8Array(u32Array.length * 4)

for (let i = 0; i < u32Array.length; i++) {
uint8Array[i * 4] = u32Array[i] & 0xff
uint8Array[i * 4 + 1] = (u32Array[i] >> 8) & 0xff
uint8Array[i * 4 + 2] = (u32Array[i] >> 16) & 0xff
uint8Array[i * 4 + 3] = (u32Array[i] >> 24) & 0xff
}

return uint8Array
}

/**
* Adds padding to the end of the given array
*
Expand Down Expand Up @@ -129,11 +148,16 @@ function preProcess(message) {
* For more info: https://en.wikipedia.org/wiki/MD5
*
* @param {Uint8Array} message - message to hash
* @return {Uint32Array} - message digest (hash value)
* @return {Uint8Array} - message digest (hash value)
*/
function MD5(message) {
// Initialize variables:
let [a0, b0, c0, d0] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
let [a0, b0, c0, d0] = [
0x67452301 >>> 0,
0xefcdab89 >>> 0,
0x98badcfe >>> 0,
0x10325476 >>> 0
]

// pre-process message and split into 512 bit chunks
const words = Array.from(preProcess(message))
Expand All @@ -160,21 +184,21 @@ function MD5(message) {
g = (7 * i) % 16
}

F = F + A + K[i] + chunk[g]
F = (F + A + K[i] + chunk[g]) >>> 0
A = D
D = C
C = B
B = (B + (rotateLeft(F, S[i]) % 0xFFFFFFFF)) >>> 0
B = ((B + rotateLeft(F, S[i])) & 0xffffffff) >>> 0
}

// add values for this chunk to main hash variables (unsigned)
a0 = a0 + A
b0 = b0 + B
c0 = c0 + C
d0 = d0 + D
a0 = (a0 + A) >>> 0
b0 = (b0 + B) >>> 0
c0 = (c0 + C) >>> 0
d0 = (d0 + D) >>> 0
})

return [a0, b0, c0, d0]
return u32ToU8([a0, b0, c0, d0])
}

// export MD5 function
Expand Down
37 changes: 37 additions & 0 deletions Hashes/tests/MD5.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { MD5 } from '../MD5'

describe('Testing MD5 function', () => {
it('should return the correct hash for "The quick brown fox jumps over the lazy dog"', () => {
const input = new TextEncoder().encode(
'The quick brown fox jumps over the lazy dog'
)

const hash = Array.from(MD5(input), (byte) =>
byte.toString(16).padStart(2, '0')
).join('')

expect(hash).toBe('9e107d9d372bb6826bd81d3542a419d6')
})

it('should return the correct hash for "The quick brown fox jumps over the lazy dog."', () => {
const input = new TextEncoder().encode(
'The quick brown fox jumps over the lazy dog.'
)

const hash = Array.from(MD5(input), (byte) =>
byte.toString(16).padStart(2, '0')
).join('')

expect(hash).toBe('e4d909c290d0fb1ca068ffaddf22cbd0')
})

it('should correctly hash an empty string', () => {
const input = new TextEncoder().encode('')

const hash = Array.from(MD5(input), (byte) =>
byte.toString(16).padStart(2, '0')
).join('')

expect(hash).toBe('d41d8cd98f00b204e9800998ecf8427e')
})
})