UNPKG

crypto-es

Version:

A cryptography algorithms library compatible with ES6 and TypeScript

1 lines 2.64 kB
{"version":3,"file":"mode-cfb.cjs","names":["keystream: number[]","BlockCipherMode"],"sources":["../src/mode-cfb.ts"],"sourcesContent":["import {\n BlockCipherMode,\n Cipher,\n} from './cipher-core';\n\nfunction generateKeystreamAndEncrypt(\n this: BlockCipherMode,\n words: number[],\n offset: number,\n blockSize: number,\n cipher: Cipher\n): void {\n const _words = words;\n let keystream: number[];\n\n // Shortcut\n const iv = this._iv;\n\n // Generate keystream\n if (iv) {\n keystream = iv.slice(0);\n\n // Remove IV for subsequent blocks\n this._iv = undefined;\n } else {\n keystream = this._prevBlock!;\n }\n cipher.encryptBlock!(keystream, 0);\n\n // Encrypt\n for (let i = 0; i < blockSize; i += 1) {\n _words[offset + i] ^= keystream[i];\n }\n}\n\n/**\n * CFB Encryptor\n */\nclass CFBEncryptor extends BlockCipherMode {\n processBlock(words: number[], offset: number): void {\n // Shortcuts\n const cipher = this._cipher;\n const blockSize = cipher.blockSize!;\n\n generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);\n\n // Remember this block to use with next block\n this._prevBlock = words.slice(offset, offset + blockSize);\n }\n}\n\n/**\n * CFB Decryptor\n */\nclass CFBDecryptor extends BlockCipherMode {\n processBlock(words: number[], offset: number): void {\n // Shortcuts\n const cipher = this._cipher;\n const blockSize = cipher.blockSize!;\n\n // Remember this block to use with next block\n const thisBlock = words.slice(offset, offset + blockSize);\n\n generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);\n\n // This block becomes the previous block\n this._prevBlock = thisBlock;\n }\n}\n\n/**\n * Cipher Feedback block mode.\n */\nexport class CFB extends BlockCipherMode {\n static readonly Encryptor = CFBEncryptor;\n static readonly Decryptor = CFBDecryptor;\n}"],"mappings":";;;AAKA,SAAS,4BAEP,OACA,QACA,WACA,QACM;CACN,MAAM,SAAS;CACf,IAAIA;CAGJ,MAAM,KAAK,KAAK;AAGhB,KAAI,IAAI;AACN,cAAY,GAAG,MAAM;AAGrB,OAAK,MAAM;CACZ,MACC,aAAY,KAAK;AAEnB,QAAO,aAAc,WAAW;AAGhC,MAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAAK,EAClC,QAAO,SAAS,MAAM,UAAU;AAEnC;;;;AAKD,IAAM,eAAN,cAA2BC,oCAAgB;CACzC,aAAa,OAAiB,QAAsB;EAElD,MAAM,SAAS,KAAK;EACpB,MAAM,YAAY,OAAO;AAEzB,8BAA4B,KAAK,MAAM,OAAO,QAAQ,WAAW;AAGjE,OAAK,aAAa,MAAM,MAAM,QAAQ,SAAS;CAChD;AACF;;;;AAKD,IAAM,eAAN,cAA2BA,oCAAgB;CACzC,aAAa,OAAiB,QAAsB;EAElD,MAAM,SAAS,KAAK;EACpB,MAAM,YAAY,OAAO;EAGzB,MAAM,YAAY,MAAM,MAAM,QAAQ,SAAS;AAE/C,8BAA4B,KAAK,MAAM,OAAO,QAAQ,WAAW;AAGjE,OAAK,aAAa;CACnB;AACF;;;;AAKD,IAAa,MAAb,cAAyBA,oCAAgB;CACvC,OAAgB,YAAY;CAC5B,OAAgB,YAAY;AAC7B"}