プレイグラウンド、サンドボックス、使い捨てスクリプト置き場
0

Configure Feed

Select the types of activity you want to include in your feed.

create jose

Kohei Watanabe (Dec 3, 2024, 12:57 PM +0900) 3d8c2deb 20467369

+60
+5
jose/deno.json
··· 1 + { 2 + "imports": { 3 + "jose": "npm:jose@^5.9.6" 4 + } 5 + }
+17
jose/deno.lock
··· 1 + { 2 + "version": "4", 3 + "specifiers": { 4 + "npm:jose@*": "5.9.6", 5 + "npm:jose@^5.9.6": "5.9.6" 6 + }, 7 + "npm": { 8 + "jose@5.9.6": { 9 + "integrity": "sha512-AMlnetc9+CV9asI19zHmrgS/WYsWUwCn2R7RzlbJWD7F9eWYUTGyBmU9o6PxngtLGOiDGPRu+Uc4fhKzbpteZQ==" 10 + } 11 + }, 12 + "workspace": { 13 + "dependencies": [ 14 + "npm:jose@^5.9.6" 15 + ] 16 + } 17 + }
+38
jose/jwe.ts
··· 1 + import { 2 + compactDecrypt, 3 + CompactEncrypt, 4 + exportJWK, 5 + generateKeyPair, 6 + importJWK, 7 + } from "npm:jose"; 8 + 9 + /* 10 + * JWE_SECRET=$(openssl rand -base64 32) deno run -A jwe.ts 11 + */ 12 + 13 + const encryptionKey = await importJWK({ 14 + kty: "oct", 15 + k: Deno.env.get("JWE_SECRET"), 16 + }); 17 + 18 + const keyToEncrypt = await generateKeyPair("ES256", { extractable: true }); 19 + const privateKeyJWK = await exportJWK(keyToEncrypt.privateKey); 20 + 21 + // encrypt 22 + const jwe = await new CompactEncrypt( 23 + new TextEncoder().encode(JSON.stringify(privateKeyJWK)), 24 + ) 25 + .setProtectedHeader({ 26 + alg: "dir", 27 + enc: "A256GCM", 28 + }) 29 + .encrypt(encryptionKey); 30 + 31 + // decrypt 32 + const res = await compactDecrypt(jwe, encryptionKey); 33 + const jwk = JSON.parse(new TextDecoder().decode(res.plaintext)); 34 + 35 + console.log({ 36 + jwe, 37 + jwk, 38 + });