UNPKG

@guarani/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

51 lines (50 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dir = void 0; const invalid_json_web_encryption_exception_1 = require("../../../exceptions/invalid-json-web-encryption.exception"); const jsonwebencryption_keywrap_algorithm_1 = require("./jsonwebencryption-keywrap.algorithm"); /** * Implementation of the dir JSON Web Encryption Key Wrap Algorithm. */ class DirKeyWrapAlgorithm extends jsonwebencryption_keywrap_algorithm_1.JsonWebEncryptionKeyWrapAlgorithm { /** * Instantiates a new JSON Web Encryption dir Key Wrap Algorithm to Wrap and Unwrap Content Encryption Keys. */ constructor() { super('dir', 'oct'); } /** * Returns an empty Buffer since the Algorithm does not Wrap the provided Content Encryption Key. * * @param enc JSON Web Encryption Content Encryption Algorithm. * @param key JSON Web Key to be used as the Content Encryption Key used to Encrypt the Plaintext. * @returns Empty Buffer as the Wrapped Content Encryption Key. */ async wrap(enc, key) { this.validateJsonWebKey(key); const cek = key.export({ encoding: 'buffer' }); const ek = Buffer.alloc(0); enc.validateContentEncryptionKey(cek); return { cek, ek }; } /** * Returns the provided JSON Web Key as the Content Encryption Key. * * @param enc JSON Web Encryption Content Encryption Algorithm. * @param key JSON Web Key used as the Content Encryption Key. * @param ek ~Wrapped Content Encryption Key~. * @returns Provided JSON Web Key as the Content Encryption Key. */ async unwrap(enc, key, ek) { if (ek.length !== 0) { throw new invalid_json_web_encryption_exception_1.InvalidJsonWebEncryptionException('Expected the Encrypted Content Encryption Key to be empty.'); } const cek = key.export({ encoding: 'buffer' }); enc.validateContentEncryptionKey(cek); return cek; } } /** * Direct use of a shared symmetric key as the CEK. */ exports.dir = new DirKeyWrapAlgorithm();