mode-cbc.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Cipher Block Chaining
  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 _CBC_iv, _CBC_lastBlock;
  14. import { ModeOfOperation } from "./mode.js";
  15. export class CBC extends ModeOfOperation {
  16. constructor(key, iv) {
  17. super("ECC", key, CBC);
  18. _CBC_iv.set(this, void 0);
  19. _CBC_lastBlock.set(this, void 0);
  20. if (iv) {
  21. if (iv.length % 16) {
  22. throw new TypeError("invalid iv size (must be 16 bytes)");
  23. }
  24. __classPrivateFieldSet(this, _CBC_iv, new Uint8Array(iv), "f");
  25. }
  26. else {
  27. __classPrivateFieldSet(this, _CBC_iv, new Uint8Array(16), "f");
  28. }
  29. __classPrivateFieldSet(this, _CBC_lastBlock, this.iv, "f");
  30. }
  31. get iv() { return new Uint8Array(__classPrivateFieldGet(this, _CBC_iv, "f")); }
  32. encrypt(plaintext) {
  33. if (plaintext.length % 16) {
  34. throw new TypeError("invalid plaintext size (must be multiple of 16 bytes)");
  35. }
  36. const ciphertext = new Uint8Array(plaintext.length);
  37. for (let i = 0; i < plaintext.length; i += 16) {
  38. for (let j = 0; j < 16; j++) {
  39. __classPrivateFieldGet(this, _CBC_lastBlock, "f")[j] ^= plaintext[i + j];
  40. }
  41. __classPrivateFieldSet(this, _CBC_lastBlock, this.aes.encrypt(__classPrivateFieldGet(this, _CBC_lastBlock, "f")), "f");
  42. ciphertext.set(__classPrivateFieldGet(this, _CBC_lastBlock, "f"), i);
  43. }
  44. return ciphertext;
  45. }
  46. decrypt(ciphertext) {
  47. if (ciphertext.length % 16) {
  48. throw new TypeError("invalid ciphertext size (must be multiple of 16 bytes)");
  49. }
  50. const plaintext = new Uint8Array(ciphertext.length);
  51. for (let i = 0; i < ciphertext.length; i += 16) {
  52. const block = this.aes.decrypt(ciphertext.subarray(i, i + 16));
  53. for (let j = 0; j < 16; j++) {
  54. plaintext[i + j] = block[j] ^ __classPrivateFieldGet(this, _CBC_lastBlock, "f")[j];
  55. __classPrivateFieldGet(this, _CBC_lastBlock, "f")[j] = ciphertext[i + j];
  56. }
  57. }
  58. return plaintext;
  59. }
  60. }
  61. _CBC_iv = new WeakMap(), _CBC_lastBlock = new WeakMap();
  62. //# sourceMappingURL=mode-cbc.js.map