我需要使用私钥从字符串创建 HmacSHA256... 我使用react-native-crypto-js,但我不能使用它的HmacSHA256方法, 它总是给我“未定义的函数”错误,这是我的代码:
const signature = CryptoJS.HmacSHA256('simple', '123456789');
const signatureBase = signature.toString(CryptoJS.enc.Base64);
我也遵循了它的文档,但仍然遇到相同的错误, 如果您知道其他解决方案或使用此软件包的正确方法,请帮助我。 谢谢。
我找到了 react 本机 Windows HmacSHA256 算法的解决方法。我已经用了2个套餐了
第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)
第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));
使用加密js。
如何安装这里
使用方法:
import base64 from 'crypto-js/enc-base64';
import hmac from 'crypto-js/hmac-sha256';
const sig = hmac(str, key).toString(base64);