【发布时间】:2021-10-07 06:20:27
【问题描述】:
我想在客户端设备上使用通过 Ed25519 生成的私钥签署 JWS(json Web 签名)。然后将此签名发送到我的后端并使用公钥进行验证。
为了熟悉该过程,我想尝试在节点 js 中签名和验证 JWS。
我的私钥和公钥都已经生成并且在 base58 中可用。这是我目前使用 Ed25519 私钥签署 JWT 的尝试:
const { SignJWT } = require("jose/jwt/sign");
const bs50 = require("bs58");
async function main() {
const publicBase58 = "A77GCUCZ7FAuXVMKtwwXyFhMa158XsaoGKHYNnJ1q3pv";
const privateKeyBase58 = "BE1VM7rTRJReLsTLLG4JMNX5ozcp7qpmMuRht9zB1UjU";
const publicKeyBuffer = bs50.decode(publicBase58);
const privateKeyBuffer = bs50.decode(privateKeyBase58);
const publicKey = new Uint8Array(publicKeyBuffer);
const privateKey = new Uint8Array(privateKeyBuffer);
const jwt = await new SignJWT({
subject: "uuid",
})
.setProtectedHeader({ alg: "EdDSA" })
.setExpirationTime("2h")
.sign(privateKey);
console.log(jwt);
}
错误:TypeError:密钥必须是 KeyObject 或 CryptoKey 类型之一。收到一个 Uint8Array 的实例
当尝试使用 sign() 函数时,我收到上述错误,因为我的 privateKey 是 Uint8Array 类型,唯一接受的类型是 KeyObject 或 CryptoKey 但我不知道如何转换我的Uint8Arrays 转换成KeyObjects 或CryptoKeys。
我从这个answer得到了一些代码sn-ps
【问题讨论】:
-
如何导入
subtile模块。我已经尝试过const { subtle } = require('crypto').webcrypto;
标签: javascript node.js jwt ed25519