Compute HMAC_SHA256 in JavaScript?

Viewed 4350

I want to compute the SHA256 based HMAC for some key and a message in front-end using JavaScript. It's easily done in Python like so:

import hmac
h = hmac.new(b'key', b'message', 'sha256')
print(h.hexdigest())

I searched and found something in NodeJS but can't do the same thing in front-end (I mean in browser of the user), which I guess I need to bundle the required library and do the stuff but could not figure out how.

1 Answers

npm i js-sha256 [link]

will install it on the domain of your choice, in node_modules/ and you can link it from there.

console.log(sha256.hmac('key', 'message'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-sha256/0.9.0/sha256.min.js"></script>

Related