UNPKG

2.1 kBJavaScriptView Raw
1;(function (root, factory, undef) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"), require("./cipher-core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core", "./cipher-core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 /**
17 * Cipher Feedback block mode.
18 */
19 CryptoJS.mode.CFB = (function () {
20 var CFB = CryptoJS.lib.BlockCipherMode.extend();
21
22 CFB.Encryptor = CFB.extend({
23 processBlock: function (words, offset) {
24 // Shortcuts
25 var cipher = this._cipher;
26 var blockSize = cipher.blockSize;
27
28 generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
29
30 // Remember this block to use with next block
31 this._prevBlock = words.slice(offset, offset + blockSize);
32 }
33 });
34
35 CFB.Decryptor = CFB.extend({
36 processBlock: function (words, offset) {
37 // Shortcuts
38 var cipher = this._cipher;
39 var blockSize = cipher.blockSize;
40
41 // Remember this block to use with next block
42 var thisBlock = words.slice(offset, offset + blockSize);
43
44 generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
45
46 // This block becomes the previous block
47 this._prevBlock = thisBlock;
48 }
49 });
50
51 function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) {
52 // Shortcut
53 var iv = this._iv;
54
55 // Generate keystream
56 if (iv) {
57 var keystream = iv.slice(0);
58
59 // Remove IV for subsequent blocks
60 this._iv = undefined;
61 } else {
62 var keystream = this._prevBlock;
63 }
64 cipher.encryptBlock(keystream, 0);
65
66 // Encrypt
67 for (var i = 0; i < blockSize; i++) {
68 words[offset + i] ^= keystream[i];
69 }
70 }
71
72 return CFB;
73 }());
74
75
76 return CryptoJS.mode.CFB;
77
78}));
\No newline at end of file