encrypt.js 881 B

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