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内已经实现的方法。
第一步生成md5 hash串:
var reader = new FileReader()
reader.onload = function(callback) {
var md5 = rstr2hex(binl2rstr(binl_md5(reader.result, reader.result.length)))
}
document.getElementById("xxx").value=md5
第二步:获取生成的md5值
var md5value = document.getElementById('xxx').value
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条)