21

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");

How do I perform this task on nodejs JavaScript

5
  • Most browsers have a Crypto API and there's a SHA256 demo If you want to use a third-party library, you need to specify which library you used. Commented Apr 30, 2019 at 17:54
  • 1
    sha512 github page says This library is deprecated. While browsing npm atleast check if it's actively maintained. github.com/cryptocoinjs/sha512 Commented Apr 30, 2019 at 17:57
  • Yes I remembered I logged this deprecation error earlier and I was wondering why because I've tried different libraries on NPM. I'll try cryptocoinjs out. Commented Apr 30, 2019 at 18:02
  • @Lewis Apologies for the accusation! Yes it's a warning that breaks the API. The code literally stops running at that point and won't return anything. I'll have to make changes and restart the server to make it work again. Again I'm sorry for accusing you wrongly. Commented May 1, 2019 at 12:39
  • @Lewis thanks! I'll do the same Commented May 1, 2019 at 12:44

2 Answers 2

58

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:

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
Sign up to request clarification or add additional context in comments.

10 Comments

why does it say sha256, when OP asked for sha512?
@BlueRineS Edited.
Note: Chrome thinks that you do not deserve to use hashing functions if you can't make yourself an SSL certificate for things running over your intranet. w3.org/2016/08/29-crypto-minutes.html
In the map function call, do we really need to do ('00' + x.toString(16)).slice(-2)? Wouldn't slice(-2), return the same result irrespectively?
If x.toString() is single-digit, the '00' + inserts an extra 0 to make sure the map call always returns two characters. But I see no reason for there to be two zeroes in the string, because even if x was 0, adding a single 9 would suffice to make it 2 zeroes.
The posted code is quite similar to the example on mdn (which isn't meant as critique, just an observation) and I found the mdn example has better readability, so I ended up using the mdn snippet. Plus the code posted here has some strange unexplained or slightly incorrect artifacts: the TextEncoder ctor doesn't have any params, the .slice(-2) isn't explained and doesn't look right, the Array.prototype.map.call() I'd rather replace with Array.from() and .map() for sanity.
|
0

You can use js-sha512 package from NPM if you are working on environment that does not have standard crypto properties.

Example:

import { sha512 } from "js-sha512";

const originalMessage = "hello"
const hash = sha512.digest(secretKey, str);

Side note: you will get different result if you hash the message with HMAC. HMAC includes your secret into hash.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.