Quiero firmar un JWS (firma web json) con una clave privada generada a través de Ed25519 en el dispositivo de un cliente. Luego envíe esta firma a mi backend y verifíquela con la clave pública. Para familiarizarme con el procedimiento, quiero intentar firmar y verificar un JWS en el nodo js.
Tanto mi clave privada como la pública ya están generadas y están disponibles en base58. Este es mi intento actual de firmar un JWT con una clave privada Ed25519:
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); }Error: TypeError: la clave debe ser del tipo KeyObject o CryptoKey. Recibió una instancia de Uint8Array
Cuando trato de usar la función sign() , aparece el error anterior porque mi clave privada es del tipo Uint8Array , los únicos tipos aceptados son KeyObject o CryptoKey , pero no sé cómo puedo convertir mis Uint8Arrays en KeyObjects o CryptoKeys .
Obtuve algunos fragmentos de código de esta respuesta
Necesita sus claves en un formato que Node.js reconozca. KeyObject create*Key Las API reconocen y la clave es compatible con las claves Ed25519, es decir, suponiendo que Node.js >= 16.0.0:
Aquí hay un fragmento que usa DER.
import { SignJWT, jwtVerify } from "jose" import bs58 from "bs58" import { createPrivateKey, createPublicKey } from "crypto" (async function main() { const publicBase58 = "A77GCUCZ7FAuXVMKtwwXyFhMa158XsaoGKHYNnJ1q3pv"; const privateKeyBase58 = "BE1VM7rTRJReLsTLLG4JMNX5ozcp7qpmMuRht9zB1UjU"; let publicKey = bs58.decode(publicBase58); let privateKey = bs58.decode(privateKeyBase58); publicKey = createPublicKey({ key: Buffer.concat([Buffer.from("302a300506032b6570032100", "hex"), publicKey]), format: "der", type: "spki", }); privateKey = createPrivateKey({ key: Buffer.concat([ Buffer.from("302e020100300506032b657004220420", "hex"), privateKey, ]), format: "der", type: "pkcs8", }) const jwt = await new SignJWT({ subject: "uuid", }) .setProtectedHeader({ alg: "EdDSA" }) .setExpirationTime("2h") .sign(privateKey); console.log(await jwtVerify(jwt, publicKey)) })()Aquí hay uno que usa JWK.
import { SignJWT, jwtVerify } from "jose" import bs58 from "bs58" import { createPrivateKey, createPublicKey } from "crypto" (async function main() { const publicBase58 = "A77GCUCZ7FAuXVMKtwwXyFhMa158XsaoGKHYNnJ1q3pv"; const privateKeyBase58 = "BE1VM7rTRJReLsTLLG4JMNX5ozcp7qpmMuRht9zB1UjU"; let publicKey = bs58.decode(publicBase58); let privateKey = bs58.decode(privateKeyBase58); publicKey = createPublicKey({ key: { kty: "OKP", crv: "Ed25519", x: publicKey.toString("base64url") }, format: "jwk" }); privateKey = createPrivateKey({ key: { kty: "OKP", crv: "Ed25519", x: publicKey.toString("base64url"), d: privateKey.toString("base64url"), }, format: "jwk" }) const jwt = await new SignJWT({ subject: "uuid", }) .setProtectedHeader({ alg: "EdDSA" }) .setExpirationTime("2h") .sign(privateKey); console.log(await jwtVerify(jwt, publicKey)) })()