Node JS使用加密计算MD5哈希的不同结果
Different result in NodeJS calculating MD5 hash using crypo
我正在尝试使用加密从 NodeJS 中的一个数字中获取 MD5 has,但我得到了一个不同的哈希返回值,然后我从可以计算 has 的站点获取。
根据 http://onlinemd5.com/ 1092000 的 MD5 是 AF118C8D2A0D27A1D49582FDF6339B7C。
当我尝试在 NodeJS 中计算该数字的哈希值时,它给出了不同的结果 (ac4d61a5b76c96b00235a124dfd1bfd1)。我的代码:
const crypto = require('crypto');
const num = 1092000;
const hash = crypto.createHash('md5').update(toString(num)).digest('hex');
console.log(hash);
如果您将它正常转换为字符串,它会起作用:
const hash = crypto.createHash('md5').update(String(num)).digest('hex'); // or num.toString()
看区别:
toString(num) = [object Undefined]
(1092000).toString() = "1092000"
如果您在默认情况下 console.log(this)
节点环境中,您将看到它是:
this = {} typeof = 'object'
this
在节点环境中指向 module.exports
所以你在 Object.prototype
上调用这个 toString 这不是做字符串的正确方法module.exports
.
以外的任何转换
我正在尝试使用加密从 NodeJS 中的一个数字中获取 MD5 has,但我得到了一个不同的哈希返回值,然后我从可以计算 has 的站点获取。
根据 http://onlinemd5.com/ 1092000 的 MD5 是 AF118C8D2A0D27A1D49582FDF6339B7C。
当我尝试在 NodeJS 中计算该数字的哈希值时,它给出了不同的结果 (ac4d61a5b76c96b00235a124dfd1bfd1)。我的代码:
const crypto = require('crypto');
const num = 1092000;
const hash = crypto.createHash('md5').update(toString(num)).digest('hex');
console.log(hash);
如果您将它正常转换为字符串,它会起作用:
const hash = crypto.createHash('md5').update(String(num)).digest('hex'); // or num.toString()
看区别:
toString(num) = [object Undefined]
(1092000).toString() = "1092000"
如果您在默认情况下 console.log(this)
节点环境中,您将看到它是:
this = {} typeof = 'object'
this
在节点环境中指向 module.exports
所以你在 Object.prototype
上调用这个 toString 这不是做字符串的正确方法module.exports
.