crypto-es
Version:
A cryptography algorithms library compatible with ES6 and TypeScript
1 lines • 1.75 kB
Source Map (JSON)
{"version":3,"file":"mode-ctr.mjs","names":[],"sources":["../src/mode-ctr.ts"],"sourcesContent":["import {\n BlockCipherMode,\n} from './cipher-core';\n\n/**\n * CTR Encryptor/Decryptor (same operation)\n */\nclass CTRMode extends BlockCipherMode {\n /** Counter for CTR mode */\n _counter?: number[];\n\n processBlock(words: number[], offset: number): void {\n const _words = words;\n\n // Shortcuts\n const cipher = this._cipher;\n const blockSize = cipher.blockSize!;\n const iv = this._iv;\n let counter = this._counter;\n\n // Generate keystream\n if (iv) {\n this._counter = iv.slice(0);\n counter = this._counter;\n\n // Remove IV for subsequent blocks\n this._iv = undefined;\n }\n const keystream = counter!.slice(0);\n cipher.encryptBlock!(keystream, 0);\n\n // Increment counter\n counter![blockSize - 1] = (counter![blockSize - 1] + 1) | 0;\n\n // Encrypt\n for (let i = 0; i < blockSize; i += 1) {\n _words[offset + i] ^= keystream[i];\n }\n }\n}\n\n/**\n * Counter block mode.\n */\nexport class CTR extends BlockCipherMode {\n /** Counter for CTR mode */\n _counter?: number[];\n\n static readonly Encryptor = CTRMode;\n static readonly Decryptor = CTRMode;\n}"],"mappings":";;;;;;AAOA,IAAM,UAAN,cAAsB,gBAAgB;;CAEpC;CAEA,aAAa,OAAiB,QAAsB;EAClD,MAAM,SAAS;EAGf,MAAM,SAAS,KAAK;EACpB,MAAM,YAAY,OAAO;EACzB,MAAM,KAAK,KAAK;EAChB,IAAI,UAAU,KAAK;AAGnB,MAAI,IAAI;AACN,QAAK,WAAW,GAAG,MAAM;AACzB,aAAU,KAAK;AAGf,QAAK,MAAM;EACZ;EACD,MAAM,YAAY,QAAS,MAAM;AACjC,SAAO,aAAc,WAAW;AAGhC,UAAS,YAAY,KAAM,QAAS,YAAY,KAAK,IAAK;AAG1D,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAAK,EAClC,QAAO,SAAS,MAAM,UAAU;CAEnC;AACF;;;;AAKD,IAAa,MAAb,cAAyB,gBAAgB;;CAEvC;CAEA,OAAgB,YAAY;CAC5B,OAAgB,YAAY;AAC7B"}