【问题标题】:How can I generate HMAC in React-Native?如何在 React-Native 中生成 HMAC?
【发布时间】:2026-01-30 20:50:02
【问题描述】:

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

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

我也遵循了它的文档,但仍然遇到同样的错误, 如果您知道其他解决方案或使用此软件包的正确方法,请帮助我。 谢谢。

【问题讨论】:

  • 嘿,我想检查一下,如果您找到上述问题的解决方案。让我知道。

标签: react-native encryption cryptojs


【解决方案1】:

我为 react native 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));

【讨论】:

    最近更新 更多