UNPKG

jose

Version:

Universal 'JSON Web Almost Everything' - JWA, JWS, JWE, JWT, JWK with no dependencies

24 lines (23 loc) 1.03 kB
import encrypt from './encrypt.js'; import decrypt from './decrypt.js'; import ivFactory from '../lib/iv.js'; import random from './random.js'; import { encode as base64url } from './base64url.js'; import { isCryptoKey, getKeyObject } from './webcrypto.js'; const generateIv = ivFactory(random); export const wrap = async (alg, key, cek, iv) => { const jweAlgorithm = alg.substr(0, 7); iv || (iv = generateIv(jweAlgorithm)); if (isCryptoKey(key)) { key = getKeyObject(key); } const { ciphertext: encryptedKey, tag } = await encrypt(jweAlgorithm, cek, key instanceof Uint8Array ? key : key.export(), iv, new Uint8Array(0)); return { encryptedKey, iv: base64url(iv), tag: base64url(tag) }; }; export const unwrap = async (alg, key, encryptedKey, iv, tag) => { const jweAlgorithm = alg.substr(0, 7); if (isCryptoKey(key)) { key = getKeyObject(key); } return decrypt(jweAlgorithm, key instanceof Uint8Array ? key : key.export(), encryptedKey, iv, tag, new Uint8Array(0)); };