emn178 / js-sha256

A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.
MIT License
870 stars 240 forks source link

How to pass the input configs Input Encoding, Output Encoding( UTF-8, HMAC ) #47

Open thangpqgm opened 2 months ago

thangpqgm commented 2 months ago

Hi, I see in your online demo tool, you can adjust Input/Output encoding and hmac Encoding, How can I do it with the hmac function in the code

import {sha256} from 'js-sha256';
console.log(sha256.hmac('key', 'value')) // <= I need to set encoding

image

Thank you!

emn178 commented 2 months ago

This library can accept binary such as bytes array or Uint8Array. See example. You can convert your data to bytes before input. This library can also output bytes and you can convert to other encodings. I use other libraries handle encoding outside this library. For example, I use hi-base64 to handle base64 encoding. You can do something like:

base64.encode(sha256.digest(''))