Files
JavaScript/Hashes/tests/MD5.test.js
Manpreet Singh 889d9c361d feat: Added MD5 hashing algorithm (#1519)
* feat: Added MD5 hashing algorithm

* Added wiki link

* Remove spam?

* Fix extend towards end

* Return Uint32Array in MD5 function

* Preprocess function now works on uint arrays

* chunkify U32Array instead of string

* Remove all string related functions

* Replace typed arrays with named variables

* Fix "Replace typed arrays with named variables"

* Return Uint8 Array in MD5 function

The MD5 function now returns a uint8 array with the correct endianness.

* Add tests

* Fix docstrings

* Introduce hexMD5 function

* Change test string

* Format test file
2023-10-30 10:54:35 +05:30

39 lines
1.1 KiB
JavaScript

import { MD5 } from '../MD5'
/**
* Returns the MD5 hash of the given message as a hexadecimal string
*
* @param {Uint8Array} message - message to hash
* @return {string} - hash as a hexadecimal string
*/
function hexMD5(message) {
return Array.from(MD5(message), (byte) =>
byte.toString(16).padStart(2, '0')
).join('')
}
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 = hexMD5(input)
expect(hash).toBe('9e107d9d372bb6826bd81d3542a419d6')
})
it('should return the correct hash for "JavaScript!"', () => {
const input = new TextEncoder().encode('JavaScript!')
const hash = hexMD5(input)
expect(hash).toBe('209eddd6b61af0643907a8e069a08fb8')
})
it('should correctly hash an empty string', () => {
const input = new TextEncoder().encode('')
const hash = hexMD5(input)
expect(hash).toBe('d41d8cd98f00b204e9800998ecf8427e')
})
})