如何使用加密从字符串创建sha256

时间:2020-09-06 00:54:28

标签: node.js expo

因此,我尝试按照此代码示例创建随机数

   let randomString = uuid();

  // create a sha256 of randomString, this will be the nonce sent to apple during authentication
  const nonce = await Crypto.digestStringAsync(
    Crypto.CryptoDigestAlgorithm.SHA256,
    randomString
  );

但是此示例适用于博览会的“ Crypto”版本,我正在使用内置于node.js的Crypto并尝试执行此操作

    let randomString = uuid(); // <--- this just creates a random string for me

   const nonce = await crypto
      .createHmac('sha256', randomString)
      .update(uuid())
      .digest('string');

但是,这似乎并没有以我需要的格式创建随机数。谁知道expo的加密货币和内置在node.js中的加密货币的人知道我在做什么错吗?我敢肯定,我不会像使用expo加密示例那样使用nodejs加密,所以也许有人认识他们都可以告诉我该expo加密示例的nodejs加密版本是什么?

1 个答案:

答案 0 :(得分:1)

摘要字符串的Expo缺省格式为十六进制(doc

选项CryptoDigestOptions摘要字符串的格式。默认值为:CryptoDigestOptions.HEX

因此,您只需将.digest("string")更改为.digest("hex")

let randomString = uuid()

const nonce = await crypto
  .createHmac("sha256", randomString)
  .update(uuid())
  .digest("hex")