Reputation: 443
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.
Upvotes: 3
Views: 5749
Reputation: 1
Use crypto-js.
How to install here
How to use:
import base64 from 'crypto-js/enc-base64';
import hmac from 'crypto-js/hmac-sha256';
const sig = hmac(str, key).toString(base64);
Upvotes: 0
Reputation: 797
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));
Upvotes: 2