mirror of
https://github.com/kou029w/_.git
synced 2025-01-30 13:58:08 +00:00
create jose
This commit is contained in:
parent
204673694b
commit
3d8c2deba7
3 changed files with 60 additions and 0 deletions
5
jose/deno.json
Normal file
5
jose/deno.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"imports": {
|
||||
"jose": "npm:jose@^5.9.6"
|
||||
}
|
||||
}
|
17
jose/deno.lock
generated
Normal file
17
jose/deno.lock
generated
Normal 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
38
jose/jwe.ts
Normal 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,
|
||||
});
|
Loading…
Add table
Reference in a new issue