Reputation: 541
I need to hash some data using MD5. I wanted to test the performance of difference libraries. However, I'm noticing that I'm getting different hashes of the same data, depending upon which library is being used:
Generating data...
Hashing data with 'md5' library...
Hash: 1b37c84ebc8426e19d3b1797b6e320c6
Duration: 0.848s
Hashing data with 'object-hash' library...
Hash: f046296e8016b5632e6a1d1ebc885892
Duration: 0.032s
Hashing data with 'create-hash' library...
Hash: 1b37c84ebc8426e19d3b1797b6e320c6
Duration: 0.023s
I would have expected the same hash to be shown for both libraries. Any ideas? My code is below...
const generateData = () => {
console.log('Generating data...')
let data = "";
for (let i = 0; i < 1000000; i++) {
data += "1234567890";
}
console.log()
return data;
}
const convertToUInt8Array = (s) => {
var result = [];
for(var i = 0; i < s.length; i+=2)
{
result.push(parseInt(s.substring(i, i + 2), 16));
}
result = Uint8Array.from(result)
return result;
}
const hashData = (name, hashingFunction, data) => {
console.log(`Hashing data with '${name}' library...`)
const start = new Date()
const hash = hashingFunction(data)
const end = new Date()
console.log(`\t Hash: ${hash}`)
const duration = (end - start) / 1000
console.log(`\t Duration: ${duration}s`)
console.log()
}
const hashWithMD5 = (data) => {
const md5 = require('md5')
return md5(data)
}
const hashWithObjectHash = (data) => {
const objectHash = require('object-hash')
return objectHash.MD5(data)
}
const hashWithCreateHash = (data) => {
const createHash = require('create-hash')
const hash = createHash('md5')
hash.update(data)
return hash.digest('hex')
}
const data = generateData()
hashData('md5', hashWithMD5, data)
hashData('object-hash', hashWithObjectHash, data)
hashData('create-hash', hashWithCreateHash, data)
Upvotes: 4
Views: 484
Reputation: 51977
object-hash
hashes the object, not the string value. The implementation prepends
'string:' + string.length + ':'
to the input string and does an MD5 hash of that. You can confirm this by modifying hashWithCreateHash
to generate a hash of the same input:
const hashWithCreateHash = (data) => {
const createHash = require('create-hash')
const hash = createHash('md5')
hash.update(`string:${data.length}:${data}`) // instead of hash.update(data)
return hash.digest('hex')
}
Hashing data with 'create-hash' library...
Hash: f046296e8016b5632e6a1d1ebc885892
Duration: 0.023s
Upvotes: 2