mode-cbc.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. // Cipher Block Chaining
  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 _CBC_iv, _CBC_lastBlock;
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.CBC = void 0;
  17. const mode_js_1 = require("./mode.js");
  18. class CBC extends mode_js_1.ModeOfOperation {
  19. constructor(key, iv) {
  20. super("ECC", key, CBC);
  21. _CBC_iv.set(this, void 0);
  22. _CBC_lastBlock.set(this, void 0);
  23. if (iv) {
  24. if (iv.length % 16) {
  25. throw new TypeError("invalid iv size (must be 16 bytes)");
  26. }
  27. __classPrivateFieldSet(this, _CBC_iv, new Uint8Array(iv), "f");
  28. }
  29. else {
  30. __classPrivateFieldSet(this, _CBC_iv, new Uint8Array(16), "f");
  31. }
  32. __classPrivateFieldSet(this, _CBC_lastBlock, this.iv, "f");
  33. }
  34. get iv() { return new Uint8Array(__classPrivateFieldGet(this, _CBC_iv, "f")); }
  35. encrypt(plaintext) {
  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.length);
  40. for (let i = 0; i < plaintext.length; i += 16) {
  41. for (let j = 0; j < 16; j++) {
  42. __classPrivateFieldGet(this, _CBC_lastBlock, "f")[j] ^= plaintext[i + j];
  43. }
  44. __classPrivateFieldSet(this, _CBC_lastBlock, this.aes.encrypt(__classPrivateFieldGet(this, _CBC_lastBlock, "f")), "f");
  45. ciphertext.set(__classPrivateFieldGet(this, _CBC_lastBlock, "f"), i);
  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. const plaintext = new Uint8Array(ciphertext.length);
  54. for (let i = 0; i < ciphertext.length; i += 16) {
  55. const block = this.aes.decrypt(ciphertext.subarray(i, i + 16));
  56. for (let j = 0; j < 16; j++) {
  57. plaintext[i + j] = block[j] ^ __classPrivateFieldGet(this, _CBC_lastBlock, "f")[j];
  58. __classPrivateFieldGet(this, _CBC_lastBlock, "f")[j] = ciphertext[i + j];
  59. }
  60. }
  61. return plaintext;
  62. }
  63. }
  64. exports.CBC = CBC;
  65. _CBC_iv = new WeakMap(), _CBC_lastBlock = new WeakMap();
  66. //# sourceMappingURL=mode-cbc.js.map