text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.Cipher = void 0;
  const tslib_1 = require("tslib");
  const core = tslib_1.__importStar(require("../core"));
  const mech_1 = require("../mech");
  class Cipher extends core.BaseObject {
      constructor(session, alg, key, lib) {
          super(lib);
          this.session = session;
          this.init(alg, key);
      }
      update(data) {
          try {
              data = Buffer.from(data);
              const enc = Buffer.alloc(data.length + 1024);
              const res = this.lib.C_EncryptUpdate(this.session.handle, data, enc);
              return res;
          }
          catch (e) {
              try {
                  this.final();
              }
              catch (e) {
              }
              throw e;
          }
      }
      final() {
          const BUF_SIZE = 4048;
          const enc = Buffer.alloc(BUF_SIZE);
          const res = this.lib.C_EncryptFinal(this.session.handle, enc);
          return res;
      }
      once(data, enc, cb) {
          const bytes = Buffer.from(data);
          if (cb) {
              this.lib.C_Encrypt(this.session.handle, bytes, enc, cb);
          }
          else {
              return this.lib.C_Encrypt(this.session.handle, bytes, enc);
          }
      }
      init(alg, key) {
          const pMech = mech_1.Mechanism.create(alg);
          this.lib.C_EncryptInit(this.session.handle, pMech, key.handle);
      }
  }
  exports.Cipher = Cipher;
  