decrypt.js 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.compactDecrypt = void 0;
  4. const decrypt_js_1 = require("../flattened/decrypt.js");
  5. const errors_js_1 = require("../../util/errors.js");
  6. const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
  7. async function compactDecrypt(jwe, key, options) {
  8. if (jwe instanceof Uint8Array) {
  9. jwe = buffer_utils_js_1.decoder.decode(jwe);
  10. }
  11. if (typeof jwe !== 'string') {
  12. throw new errors_js_1.JWEInvalid('Compact JWE must be a string or Uint8Array');
  13. }
  14. const { 0: protectedHeader, 1: encryptedKey, 2: iv, 3: ciphertext, 4: tag, length, } = jwe.split('.');
  15. if (length !== 5) {
  16. throw new errors_js_1.JWEInvalid('Invalid Compact JWE');
  17. }
  18. const decrypted = await (0, decrypt_js_1.flattenedDecrypt)({
  19. ciphertext,
  20. iv: (iv || undefined),
  21. protected: protectedHeader || undefined,
  22. tag: (tag || undefined),
  23. encrypted_key: encryptedKey || undefined,
  24. }, key, options);
  25. const result = { plaintext: decrypted.plaintext, protectedHeader: decrypted.protectedHeader };
  26. if (typeof key === 'function') {
  27. return { ...result, key: decrypted.key };
  28. }
  29. return result;
  30. }
  31. exports.compactDecrypt = compactDecrypt;