Milind
Milind

Reputation: 493

Incorrect SHA256 from nodejs crypto

const crypto = require('crypto');

hm = crypto.createHmac("sha256","Some String");
console.log(hm.digest("base64"));

Running this gives me:

Nd6Q8epsIBG+c/jN6TdnfRNbFWCcB7bI0DYkfyDqf+8=

(repl)

But calculating the sha256 at https://approsto.com/sha-generator/ gives me:

fw/WRlO6C7Glec7Str83XpFsxgZiEJ7gwLJPCnUMOmw

Why is there a difference?

Upvotes: 1

Views: 1637

Answers (1)

John Kugelman
John Kugelman

Reputation: 361605

Use Hash instead of Hmac.

const crypto = require('crypto');

hash = crypto.createHash("sha256");
hash.update("Some String");
console.log(hash.digest("base64"));

Result:

fw/WRlO6C7Glec7Str83XpFsxgZiEJ7gwLJPCnUMOmw=

See also:

Upvotes: 4

Related Questions