React native 如何在React Native中生成HMAC?

React native 如何在React Native中生成HMAC?,react-native,encryption,cryptojs,React Native,Encryption,Cryptojs,我需要从带有私钥的字符串创建HmacSHA256。。。 我使用react native crypto js,但我不能使用它的HmacSHA256方法, 它不断出现“未定义函数”错误,以下是我的代码: const signature = CryptoJS.HmacSHA256('simple', '123456789'); const signatureBase = signature.toString(CryptoJS.enc.Base64); 我也遵循了它的文档,但仍然得到相同的错误

我需要从带有私钥的字符串创建HmacSHA256。。。 我使用react native crypto js,但我不能使用它的HmacSHA256方法, 它不断出现“未定义函数”错误,以下是我的代码:

const signature = CryptoJS.HmacSHA256('simple', '123456789');
    const signatureBase = signature.toString(CryptoJS.enc.Base64);
我也遵循了它的文档,但仍然得到相同的错误, 如果您知道其他解决方案或使用此软件包的正确方法,请帮助我。
谢谢。

我找到了一个适用于react原生windows HmacSHA256算法的解决方案。我用了两个软件包

第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)

  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));
第二步:

  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)

  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));

嘿,我想检查一下,你是否找到了上述问题的解决方案。让我知道。