mode-ofb.js 3.3 KB

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