mirror of
https://github.com/kou029w/_.git
synced 2025-01-30 13:58:08 +00:00
38 lines
785 B
TypeScript
38 lines
785 B
TypeScript
import {
|
|
exportJWK,
|
|
flattenedDecrypt,
|
|
FlattenedEncrypt,
|
|
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
|
|
const jwe = await new FlattenedEncrypt(
|
|
new TextEncoder().encode(JSON.stringify(privateKeyJWK)),
|
|
)
|
|
.setProtectedHeader({
|
|
alg: "dir",
|
|
enc: "A256GCM",
|
|
})
|
|
.encrypt(encryptionKey);
|
|
|
|
// decrypt
|
|
const res = await flattenedDecrypt(jwe, encryptionKey);
|
|
const jwk = JSON.parse(new TextDecoder().decode(res.plaintext));
|
|
|
|
console.log({
|
|
jwe,
|
|
jwk,
|
|
});
|