UNPKG

did-jwt

Version:

Library for Signing and Verifying JWTs that use DIDs as issuers and JWEs that use DIDs as recipients

46 lines • 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.xc20pEncrypter = xc20pEncrypter; exports.xc20pDirEncrypter = xc20pDirEncrypter; exports.xc20pDirDecrypter = xc20pDirDecrypter; const util_js_1 = require("../util.js"); const chacha_js_1 = require("@noble/ciphers/chacha.js"); const utils_js_1 = require("@noble/hashes/utils.js"); function xc20pEncrypter(key) { return (cleartext, aad) => { const iv = (0, utils_js_1.randomBytes)(24); const cipher = (0, chacha_js_1.xchacha20poly1305)(key, iv, aad); const sealed = cipher.encrypt(cleartext); return { ciphertext: sealed.subarray(0, sealed.length - 16), tag: sealed.subarray(sealed.length - 16), iv, }; }; } function xc20pDirEncrypter(key) { const xc20pEncrypt = xc20pEncrypter(key); const enc = 'XC20P'; const alg = 'dir'; async function encrypt(cleartext, protectedHeader = {}, aad) { const protHeader = (0, util_js_1.encodeBase64url)(JSON.stringify(Object.assign({ alg }, protectedHeader, { enc }))); const encodedAad = (0, util_js_1.stringToBytes)(aad ? `${protHeader}.${(0, util_js_1.bytesToBase64url)(aad)}` : protHeader); return { ...xc20pEncrypt(cleartext, encodedAad), protectedHeader: protHeader, }; } return { alg, enc, encrypt }; } function xc20pDirDecrypter(key) { async function decrypt(sealed, iv, aad) { try { return (0, chacha_js_1.xchacha20poly1305)(key, iv, aad).decrypt(sealed); } catch { return null; } } return { alg: 'dir', enc: 'XC20P', decrypt }; } //# sourceMappingURL=xc20pDir.js.map