mode-ofb.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Output Feedback
  2. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  3. if (kind === "m") throw new TypeError("Private method is not writable");
  4. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  5. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  6. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  7. };
  8. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  9. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  10. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  11. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  12. };
  13. var _OFB_iv, _OFB_lastPrecipher, _OFB_lastPrecipherIndex;
  14. import { ModeOfOperation } from "./mode.js";
  15. export class OFB extends ModeOfOperation {
  16. constructor(key, iv) {
  17. super("OFB", key, OFB);
  18. _OFB_iv.set(this, void 0);
  19. _OFB_lastPrecipher.set(this, void 0);
  20. _OFB_lastPrecipherIndex.set(this, void 0);
  21. if (iv) {
  22. if (iv.length % 16) {
  23. throw new TypeError("invalid iv size (must be 16 bytes)");
  24. }
  25. __classPrivateFieldSet(this, _OFB_iv, new Uint8Array(iv), "f");
  26. }
  27. else {
  28. __classPrivateFieldSet(this, _OFB_iv, new Uint8Array(16), "f");
  29. }
  30. __classPrivateFieldSet(this, _OFB_lastPrecipher, this.iv, "f");
  31. __classPrivateFieldSet(this, _OFB_lastPrecipherIndex, 16, "f");
  32. }
  33. get iv() { return new Uint8Array(__classPrivateFieldGet(this, _OFB_iv, "f")); }
  34. encrypt(plaintext) {
  35. var _a, _b;
  36. if (plaintext.length % 16) {
  37. throw new TypeError("invalid plaintext size (must be multiple of 16 bytes)");
  38. }
  39. const ciphertext = new Uint8Array(plaintext);
  40. for (let i = 0; i < ciphertext.length; i++) {
  41. if (__classPrivateFieldGet(this, _OFB_lastPrecipherIndex, "f") === 16) {
  42. __classPrivateFieldSet(this, _OFB_lastPrecipher, this.aes.encrypt(__classPrivateFieldGet(this, _OFB_lastPrecipher, "f")), "f");
  43. __classPrivateFieldSet(this, _OFB_lastPrecipherIndex, 0, "f");
  44. }
  45. ciphertext[i] ^= __classPrivateFieldGet(this, _OFB_lastPrecipher, "f")[__classPrivateFieldSet(this, _OFB_lastPrecipherIndex, (_b = __classPrivateFieldGet(this, _OFB_lastPrecipherIndex, "f"), _a = _b++, _b), "f"), _a];
  46. }
  47. return ciphertext;
  48. }
  49. decrypt(ciphertext) {
  50. if (ciphertext.length % 16) {
  51. throw new TypeError("invalid ciphertext size (must be multiple of 16 bytes)");
  52. }
  53. return this.encrypt(ciphertext);
  54. }
  55. }
  56. _OFB_iv = new WeakMap(), _OFB_lastPrecipher = new WeakMap(), _OFB_lastPrecipherIndex = new WeakMap();
  57. //# sourceMappingURL=mode-ofb.js.map