请问,nodejs MD5 要怎么弄?
发布于 12 年前 作者 workgang 74120 次浏览 最后一次编辑是 8 年前

我需要计算一个字符串的 md5 值。 像 php 就是直接提供了 md5(‘xxxxxxx’) 函数。

看了 node 的Crypto 模块。 有些摸不着头脑。

麻烦谁给解答一下。谢谢!

13 回复

你可以直接看nodeclub源代码…

var crypto = require('crypto');

exports.encrypt = function (str, secret) { var cipher = crypto.createCipher(‘aes192’, secret); var enc = cipher.update(str, ‘utf8’, ‘hex’); enc += cipher.final(‘hex’); return enc; };

exports.decrypt = function (str, secret) { var decipher = crypto.createDecipher(‘aes192’, secret); var dec = decipher.update(str, ‘hex’, ‘utf8’); dec += decipher.final(‘utf8’); return dec; };

exports.md5 = function (str) { var md5sum = crypto.createHash(‘md5’); md5sum.update(str); str = md5sum.digest(‘hex’); return str; };

exports.randomString = function (size) { size = size || 6; var code_string = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789’; var max_num = code_string.length + 1; var new_pass = ‘’; while (size > 0) { new_pass += code_string.charAt(Math.floor(Math.random() * max_num)); size–; } return new_pass; };

我照教材这样写:

var crypto = require(‘crypto’); var md5 = crypto.createHash(‘md5’); var password = md5.update(‘abcdefg’).digest(‘base64’); console.log(password);

然后输出了 esZsDxSN6VGbi9JkMSxNZA==

不过我也不太懂是什么意思,也不知道这个是不是对的,还有要怎么整的,你弄明白顺便跟我说声哈,学习学习。。。

这个学不会的人,你可以考虑为别的行业做贡献了.

楼主可以标明"新手提问"的…

var crypto = require('crypto');
function md5 (text) {
  return crypto.createHash('md5').update(text).digest('hex');
};

官方说的好明白了- -

crypto要注意中文的md5,要使用binary否则不相等

md5 不安全吧。

回到顶部