散列字符串与散列UInt8Array [英] Hashing a string vs Hashing a UInt8Array
本文介绍了散列字符串与散列UInt8Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在比较两个MD5库的性能.当我给他们两个都发送一个字符串时,它们返回相同的哈希值:
I'm comparing the performance of two MD5 libraries. When I send them both a string, they return the same hash:
Hashing data with 'md5' library...
Hash: d8b1e68f2f36743cdf302ed36f9347dc
Duration: 0.003s
Hashing data with 'create-hash' library...
Hash: d8b1e68f2f36743cdf302ed36f9347dc
Duration: 0.003s
但是,当我向他们发送相同的UInt8Array时,它们给了我不同的哈希值:
However, when I send them the same UInt8Array, they give me different hashes:
Hashing data with 'md5' library...
Hash: 77fcf76d3f8c6a0f685f784d7ca6c642
Duration: 0.001s
Hashing data with 'create-hash' library...
Hash: 0ee0646c1c77d8131cc8f4ee65c7673b
Duration: 0s
为什么会这样?
const hashData = (name, hashingFunction, data) => {
console.log(`\t Hashing data with '${name}' library...`)
const start = new Date()
const hash = hashingFunction(data)
const end = new Date()
console.log(`\t\t Hash: ${hash}`)
const duration = (end - start) / 1000
console.log(`\t\t Duration: ${duration}s`)
console.log()
}
const runHashes = (data) => {
const hashWithMD5 = (data) => {
const md5 = require('md5')
return md5(data)
}
const hashWithCreateHash = (data) => {
return require('create-hash')('md5').update(data).digest('hex')
}
hashData('md5', hashWithMD5, data)
hashData('create-hash', hashWithCreateHash, data)
}
console.log('*** Running hashes on strings... *** \n')
runHashes("I want you to hash me...")
console.log('*** Running hashes on UInt8Array... *** \n')
runHashes(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))
推荐答案
基于 String
或Buffer
.您输入的Uin8Array
都不是,因此,我猜想与正确输入给定的明确定义的输出相比,哈希的行为将相对不确定.
Based on the md5
module API documentation, the hashing function accepts either a String
or a Buffer
. Your input of Uin8Array
is neither, so I'm guessing the behavior of the hashing is going to be relatively undefined in comparison to the well-defined output given correctly typed input.
这篇关于散列字符串与散列UInt8Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文