本文介绍了如何使用带有sha512算法的JavaScript对字符串进行哈希处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试从NPM使用sha512,但它会继续散列错误的内容,即我应该获取字符串,但它会一直返回对象.因此,在PHP中,我知道我可以执行$hash = hash("sha512","my string for hashing");
I've tried using sha512 from NPM but it keeps hashing the wrong thing i.e I am supposed to get a string but it keeps returning object.So in PHP I know I can perform the task $hash = hash("sha512","my string for hashing");
如何在Node.js JavaScript上执行此任务
How do I perform this task on nodejs JavaScript
推荐答案
如果您使用的是Node:
If you are using Node:
> crypto.createHash('sha512').update('my string for hashing').digest('hex');
'4dc43467fe9140f217821252f94be94e49f963eed1889bceab83a1c36ffe3efe87334510605a9bf3b644626ac0cd0827a980b698efbc1bde75b537172ab8dbd0'
如果要使用浏览器Web Crypto API:
If you want to use the browser Web Crypto API:
function sha512(str) {
return crypto.subtle.digest("SHA-512", new TextEncoder("utf-8").encode(str)).then(buf => {
return Array.prototype.map.call(new Uint8Array(buf), x=>(('00'+x.toString(16)).slice(-2))).join('');
});
}
sha512("my string for hashing").then(x => console.log(x));
// prints: 4dc43467fe9140f217821252f94be94e49f963eed1889bceab83a1c36ffe3efe87334510605a9bf3b644626ac0cd0827a980b698efbc1bde75b537172ab8dbd0
这篇关于如何使用带有sha512算法的JavaScript对字符串进行哈希处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!