encrypt.js 897 B

123456789101112131415161718192021222324252627
  1. import { FlattenedEncrypt } from '../flattened/encrypt.js';
  2. export class CompactEncrypt {
  3. #flattened;
  4. constructor(plaintext) {
  5. this.#flattened = new FlattenedEncrypt(plaintext);
  6. }
  7. setContentEncryptionKey(cek) {
  8. this.#flattened.setContentEncryptionKey(cek);
  9. return this;
  10. }
  11. setInitializationVector(iv) {
  12. this.#flattened.setInitializationVector(iv);
  13. return this;
  14. }
  15. setProtectedHeader(protectedHeader) {
  16. this.#flattened.setProtectedHeader(protectedHeader);
  17. return this;
  18. }
  19. setKeyManagementParameters(parameters) {
  20. this.#flattened.setKeyManagementParameters(parameters);
  21. return this;
  22. }
  23. async encrypt(key, options) {
  24. const jwe = await this.#flattened.encrypt(key, options);
  25. return [jwe.protected, jwe.encrypted_key, jwe.iv, jwe.ciphertext, jwe.tag].join('.');
  26. }
  27. }