Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate HMAC in React-Native?

I need to create HmacSHA256 from a string with a private key... I use react-native-crypto-js but I cant use it's HmacSHA256 method, it keeps getting me "undefined function" error, here is my code:

const signature = CryptoJS.HmacSHA256('simple', '123456789');
    const signatureBase = signature.toString(CryptoJS.enc.Base64);

I followed it's document either but still getting same error, if you know another solution or the correct way to use this package please help me. thanks.

like image 873
Pouya92 Avatar asked Oct 20 '25 11:10

Pouya92


1 Answers

I found a work around for react native windows HmacSHA256 Algorithm. I have used 2 packages

Step 1:

  npm install --save  react-native-hash //install this
  import { JSHash, JSHmac, CONSTANTS } from "react-native-hash";

  JSHmac("message", "SecretKey", CONSTANTS.HmacAlgorithms.HmacSHA256)
  .then(hash => hmac_encoded_str=hash)
  .catch(e => console.log(e));

  OR
  let hmac_encoded_str= await JSHmac(canonical_string, "C6PqJwbyW4", 
  CONSTANTS.HmacAlgorithms.HmacSHA256)

Step 2:

  npm install react-native-crypto-js // install this as well
  
  import CryptoJS from "react-native-crypto-js"
  
  CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(hmac_encoded_str));
like image 199
bhaRATh Avatar answered Oct 23 '25 05:10

bhaRATh