求教nodejs怎么对密码进行加盐的hash加密

JavaScript07

求教nodejs怎么对密码进行加盐的hash加密,第1张

以前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)

}

CryptoJS是JavaScript的一个加解密库集合,可以说平时开发中需要用到的加解密方法,它这里都包含了

1、MD5加密,这是一种不可逆的加密

2、SHA-1加密,是现有 SHA 哈希函数中最成熟的,它用于各种安全应用程序和协议

3、SHA-2加密,不像 SHA-1 那样广泛使用,尽管它似乎提供了更好的安全性

4、AES加解密, 是美国联邦信息处理标准 (FIPS)提出的高级加密标准

5、DES加解密, DES 是以前占主导地位的加密算法,并作为官方联邦信息处理标准 (FIPS) 发布,由于密钥大小较小,DES 现在被认为是不安全的

6、转Base64

好了常用的加解密方法就介绍到这了

觉得效果不错的请帮忙加个关注点个赞,经常分享前端实用开发技巧

使用js sha512加密的方法:

1、首先去git上下载sha512.js引入需要调用的页面上。

2、调用方法,在head和</head>之间的script标签写入以下js代码:

function calcHash() {

try {

var hashInput = document.getElementById("hashInputText")

var hashInputType = document.getElementById("hashInputType")

var hashVariant = document.getElementById("hashVariant")

var hashRounds = document.getElementById("hashRounds")

var hashOutputType = document.getElementById("hashOutputType")

var hashOutput = document.getElementById("hashOutputText")

var hashObj = new jsSHA(

hashVariant.options[hashVariant.selectedIndex].value,

hashInputType.options[hashInputType.selectedIndex].value,

{numRounds: parseInt(hashRounds.value, 10)}

)

hashObj.update(hashInput.value)

hashOutput.value = hashObj.getHash(hashOutputType.options[hashOutputType.selectedIndex].value)

} catch(e) {

hashOutput.value = e.message

}

}

function calcHMAC() {

try {

var hmacText = document.getElementById("hmacInputText")

var hmacTextType = document.getElementById("hmacTextType")

var hmacKeyInput = document.getElementById("hmacInputKey")

var hmacKeyInputType = document.getElementById("hmacKeyType")

var hmacVariant = document.getElementById("hmacVariant")

var hmacOutputType = document.getElementById("hmacOutputType")

var hmacOutput = document.getElementById("hmacOutputText")

var hmacObj = new jsSHA(

hmacVariant.options[hmacVariant.selectedIndex].value,

hmacTextType.options[hmacTextType.selectedIndex].value

)

hmacObj.setHMACKey(

hmacKeyInput.value,

hmacKeyInputType.options[hmacKeyInputType.selectedIndex].value

)

hmacObj.update(hmacText.value)

hmacOutput.value = hmacObj.getHMAC(hmacOutputType.options[hmacOutputType.selectedIndex].value)

} catch(e) {

hmacOutput.value = e.message

}

}

其中jsSHA是sha512.js内已经实现的方法。