1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 13:58:08 +00:00

create jose

This commit is contained in:
Nebel 2024-12-03 12:57:03 +09:00
parent 204673694b
commit 3d8c2deba7
Signed by: nebel
GPG key ID: 79807D08C6EF6460
3 changed files with 60 additions and 0 deletions

5
jose/deno.json Normal file
View file

@ -0,0 +1,5 @@
{
"imports": {
"jose": "npm:jose@^5.9.6"
}
}

17
jose/deno.lock generated Normal file
View file

@ -0,0 +1,17 @@
{
"version": "4",
"specifiers": {
"npm:jose@*": "5.9.6",
"npm:jose@^5.9.6": "5.9.6"
},
"npm": {
"jose@5.9.6": {
"integrity": "sha512-AMlnetc9+CV9asI19zHmrgS/WYsWUwCn2R7RzlbJWD7F9eWYUTGyBmU9o6PxngtLGOiDGPRu+Uc4fhKzbpteZQ=="
}
},
"workspace": {
"dependencies": [
"npm:jose@^5.9.6"
]
}
}

38
jose/jwe.ts Normal file
View file

@ -0,0 +1,38 @@
import {
compactDecrypt,
CompactEncrypt,
exportJWK,
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 CompactEncrypt(
new TextEncoder().encode(JSON.stringify(privateKeyJWK)),
)
.setProtectedHeader({
alg: "dir",
enc: "A256GCM",
})
.encrypt(encryptionKey);
// decrypt
const res = await compactDecrypt(jwe, encryptionKey);
const jwk = JSON.parse(new TextDecoder().decode(res.plaintext));
console.log({
jwe,
jwk,
});