how to encode hmac sha256 in javascript

You can encode HMAC SHA256 in javascript using the built-in crypto module. Here's an example code snippet:

index.tsx
const crypto = require('crypto');

const message = 'Hello, world!';
const secret = 'my-secret-key';

const hash = crypto.createHmac('sha256', secret)
                   .update(message)
                   .digest('hex');

console.log(hash);
241 chars
11 lines

In this example, we first require the built-in crypto module. We then define the message and secret key that we want to encode.

Next, we create a new HMAC object by calling crypto.createHmac('sha256', secret), passing in the SHA256 algorithm and our secret key.

We then update the HMAC object with our message by calling .update(message).

Finally, we generate the HMAC hash by calling .digest('hex').

The resulting hash will be a hexadecimal string representing the encoded message using HMAC SHA256.

related categories

gistlibby LogSnag