1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-31 06:18:07 +00:00
_/jose/jwe.ts

39 lines
785 B
TypeScript
Raw Normal View History

2024-12-03 12:57:03 +09:00
import {
exportJWK,
2024-12-03 13:40:25 +09:00
flattenedDecrypt,
FlattenedEncrypt,
2024-12-03 12:57:03 +09:00
generateKeyPair,
importJWK,
} from "npm:jose";
/*
* JWE_SECRET=$(openssl rand -base64 32) deno run -A jwe.ts
*/
const encryptionKey = await importJWK({
kty: "oct",
k: Deno.env.get("JWE_SECRET"),
});
const keyToEncrypt = await generateKeyPair("ES256", { extractable: true });
const privateKeyJWK = await exportJWK(keyToEncrypt.privateKey);
// encrypt
2024-12-03 13:40:25 +09:00
const jwe = await new FlattenedEncrypt(
2024-12-03 12:57:03 +09:00
new TextEncoder().encode(JSON.stringify(privateKeyJWK)),
)
.setProtectedHeader({
alg: "dir",
enc: "A256GCM",
})
.encrypt(encryptionKey);
// decrypt
2024-12-03 13:40:25 +09:00
const res = await flattenedDecrypt(jwe, encryptionKey);
2024-12-03 12:57:03 +09:00
const jwk = JSON.parse(new TextDecoder().decode(res.plaintext));
console.log({
jwe,
jwk,
});