CNode

请问,nodejs MD5 要怎么弄?

Wworkgang发布于14 年前最后回复10 年前13 回复75793 浏览0 收藏

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

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

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

查看回复

回复 (13)

T
TigerSoaring#1·11 年前

@DavidCai1993 +1

B
blackjack#2·11 年前

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

Y
yjhjstz#4·10 年前

md5 不安全吧。

R
rekey#5·14 年前

你可以直接看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; };

I
isondada#6·14 年前

我照教材这样写:

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

然后输出了 esZsDxSN6VGbi9JkMSxNZA==

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

R
rekey#7·14 年前

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

J
jiyinyiyong#8·14 年前

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

L
leizongmin#9·14 年前
var crypto = require('crypto');
function md5 (text) {
  return crypto.createHash('md5').update(text).digest('hex');
};
X
xiaojue#10·14 年前

官方说的好明白了- -

T
tvrcgo#11·11 年前
D
DavidCai1993#12·11 年前
参与回复
登录后即可参与回复。登录