我该如何在React-Native中生成HMAC?

3

我需要使用一个私钥从一个字符串中创建HmacSHA256...我使用react-native-crypto-js,但我无法使用它的HmacSHA256方法,一直出现“未定义函数”错误,下面是我的代码:

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

我按照它的文档操作,但仍然遇到相同的错误, 如果你知道另一个解决方案或正确使用这个包的方法,请帮助我。 谢谢。


嘿,我想确认一下,你是否找到了上述问题的解决方案。请告诉我。 - Arpit Kulsreshtha
1个回答

2

我找到了一个解决方案,用于 React Native 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)

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

这些步骤之间有什么关联? - David Pears

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接