UNPKG

@veramo/did-comm

Version:

Veramo messaging plugin implementing DIDComm v2.

39 lines 1.35 kB
import { randomBytes } from '@noble/hashes/utils'; import { concat } from '@veramo/utils'; import { xchacha20poly1305 } from '@noble/ciphers/chacha'; export const xc20pKeyWrapper = { from: (wrappingKey) => { const wrap = async (cek) => { const iv = randomBytes(xchacha20poly1305.nonceLength); const cipher = xchacha20poly1305(wrappingKey, iv); const sealed = cipher.encrypt(cek); return { ciphertext: sealed.subarray(0, sealed.length - xchacha20poly1305.tagLength), tag: sealed.subarray(sealed.length - xchacha20poly1305.tagLength), iv, }; }; return { wrap }; }, alg: 'XC20PKW', }; export function xc20pDecrypter(key) { async function decrypt(sealed, iv, aad) { const cipher = xchacha20poly1305(key, iv, aad); return cipher.decrypt(sealed); } return { alg: 'dir', enc: 'XC20P', decrypt }; } export function xc20pKeyUnwrapper(wrappingKey) { const unwrap = async (wrappedCek, iv, tag) => { try { const sealed = concat([wrappedCek, tag]); return xc20pDecrypter(wrappingKey).decrypt(sealed, iv); } catch (e) { return null; } }; return { unwrap, alg: 'XC20PKW' }; } //# sourceMappingURL=xc20pkw.js.map