node.js - 如何使用加密从字符串创建 sha256
问题描述
所以我试图按照这个代码示例创建一个随机数
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
);
但是,此示例适用于世博会的“加密”版本,我正在使用 node.js 中内置的加密并尝试执行此操作
let randomString = uuid(); // <--- this just creates a random string for me
const nonce = await crypto
.createHmac('sha256', randomString)
.update(uuid())
.digest('string');
但这似乎并没有以我需要的格式创建随机数。有谁知道世博会的加密和 node.js 中内置的加密的人知道我做错了什么吗?我很确定我只是没有以与 expo 加密示例相同的方式使用 nodejs 加密,所以也许知道它们的人可以告诉我那个 expo 加密示例的 nodejs 加密版本是什么?
解决方案
摘要字符串的 Expo 默认格式为十六进制 ( doc )
options
CryptoDigestOptions
摘要字符串的格式。默认为:CryptoDigestOptions.HEX
所以你只需要更改.digest("string")
为.digest("hex")
let randomString = uuid()
const nonce = await crypto
.createHmac("sha256", randomString)
.update(uuid())
.digest("hex")
推荐阅读
- scala - 如何初始化一个类,详情如下图
- cassandra - 我想知道过去 1 小时内向 cassandra 发出的查询是什么。有没有办法得到它?
- facebook-graph-api - facebook 频道 - 机器人框架问题
- unity3d - 如何检测点击了哪个游戏对象
- html - Beautiful Soup HTML 解析异常
- azure - Azure Logic Apps Enterprise Integration Pack Visual Studio 2015 问题
- gnuplot - 在我为 y 数据设置 timefmt 后,xtic() 没有为每个条设置标签
- angular - Angular 8 Fotorama 未在底部显示预览图像
- python - 如何在没有互联网的服务器上安装 wfastcgi?
- php - 找到数字后如何获取字符串的剩余部分