我正在尝试使用加密从 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"
如果默认情况下在 Node 环境中使用 console.log(this)
,您将看到它是:
this = {} typeof = 'object'
Node 环境中的 this
指向 module.exports
所以你调用它 toString在 Object.prototype
上,除了 module.exports
之外,对任何其他内容进行字符串转换都是不正确的。
关于node.js - Node JS 使用加密计算 MD5 哈希值的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58628089/