[英]How do I hash a string using JavaScript with sha512 algorithm
I've tried using sha512 from NPM but it keeps hashing the wrong thing ie I am supposed to get a string but it keeps returning object.我尝试使用 NPM 中的 sha512,但它一直在散列错误的东西,即我应该得到一个字符串,但它一直返回 object。 So in PHP I know I can perform the task
$hash = hash("sha512","my string for hashing");
所以在 PHP 我知道我可以执行任务
$hash = hash("sha512","my string for hashing");
How do I perform this task on nodejs JavaScript如何在 nodejs JavaScript 上执行此任务
If you are using Node:如果您使用节点:
> crypto.createHash('sha512').update('my string for hashing').digest('hex');
'4dc43467fe9140f217821252f94be94e49f963eed1889bceab83a1c36ffe3efe87334510605a9bf3b644626ac0cd0827a980b698efbc1bde75b537172ab8dbd0'
If you want to use the browser Web Crypto API:如果要使用浏览器 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
you can use this library npm.io/package/js-sha512你可以使用这个库npm.io/package/js-sha512
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.