var key = '12345670'
exports.des = {
algorithm:{ ecb:'des-ecb',cbc:'des-cbc' },
encrypt:function(plaintext,iv){
var key = new Buffer(key)
var 慎返iv = new Buffer(iv ? iv : 0)
var cipher = crypto.createCipheriv(this.algorithm.ecb, 并携key, iv)
cipher.setAutoPadding(true) //default true
var ciph = cipher.update(plaintext, 'utf8', 'base64')
绝孝伏 ciph += cipher.final('base64')
return ciph
},
decrypt:function(encrypt_text,iv){
var key = new Buffer(key)
var iv = new Buffer(iv ? iv : 0)
var decipher = crypto.createDecipheriv(this.algorithm.ecb, key, iv)
decipher.setAutoPadding(true)
var txt = decipher.update(encrypt_text, 'base64', 'utf8')
txt += decipher.final('utf8')
return txt
}
}
使用require('crypto')调用加密模块。加密模块需要底层系统提供OpenSSL的支笑搏租持。它提供了一种安全凭证的封装方式,可以用于HTTPS安全网络以及普通HTTP连接。
该模块还提供了一套针对OpenSSL的hash(哈希),hmac(密钥哈希),cipher(编码),decipher(解码)碰兆,sign(签名)以及verify(验证)银誉等方法的封装。
nodejs怎么对密码进行加盐的hash加密?
以前java项目最近打算用node.js重写喊皮,但是加密这里实在没搞定。java中加密是:1024次加盐sha-1加密,
一个例子:salt:47998d63768aa877,密文:bef36ba826b045a7c5e536a2f7131a6c232eee36,明文:yunstudio2013
下面是尘渗迅java代码:
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm)
if (salt != null) {
digest.update(salt)
}
byte[] result = digest.digest(input)
for (int i = 1i <iterationsi++) {
digest.reset()
result = digest.digest(result)
}
return result
} catch (GeneralSecurityException e) {
throw Exceptions.unchecked(e)
}
}
我在js里派此面是这么干的,但是结果一直不对,代码如下:
//bef36ba826b045a7c5e536a2f7131a6c232eee36
var hash = crypto.createHmac("sha1", “47998d63768aa877”).update(“yunstudio2013”).digest(“hex”)
for (var i = 1i <1024i++) {
hash = crypto.createHmac("sha1", “47998d63768aa877”).update(hash).digest(“hex”)
console.log(hash)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)