UNPKG

3.57 kBJavaScriptView Raw
1;(function (root, factory, undef) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 (function () {
17 // Shortcuts
18 var C = CryptoJS;
19 var C_lib = C.lib;
20 var StreamCipher = C_lib.StreamCipher;
21 var C_algo = C.algo;
22
23 /**
24 * RC4 stream cipher algorithm.
25 */
26 var RC4 = C_algo.RC4 = StreamCipher.extend({
27 _doReset: function () {
28 // Shortcuts
29 var key = this._key;
30 var keyWords = key.words;
31 var keySigBytes = key.sigBytes;
32
33 // Init sbox
34 var S = this._S = [];
35 for (var i = 0; i < 256; i++) {
36 S[i] = i;
37 }
38
39 // Key setup
40 for (var i = 0, j = 0; i < 256; i++) {
41 var keyByteIndex = i % keySigBytes;
42 var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
43
44 j = (j + S[i] + keyByte) % 256;
45
46 // Swap
47 var t = S[i];
48 S[i] = S[j];
49 S[j] = t;
50 }
51
52 // Counters
53 this._i = this._j = 0;
54 },
55
56 _doProcessBlock: function (M, offset) {
57 M[offset] ^= generateKeystreamWord.call(this);
58 },
59
60 keySize: 256/32,
61
62 ivSize: 0
63 });
64
65 function generateKeystreamWord() {
66 // Shortcuts
67 var S = this._S;
68 var i = this._i;
69 var j = this._j;
70
71 // Generate keystream word
72 var keystreamWord = 0;
73 for (var n = 0; n < 4; n++) {
74 i = (i + 1) % 256;
75 j = (j + S[i]) % 256;
76
77 // Swap
78 var t = S[i];
79 S[i] = S[j];
80 S[j] = t;
81
82 keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
83 }
84
85 // Update counters
86 this._i = i;
87 this._j = j;
88
89 return keystreamWord;
90 }
91
92 /**
93 * Shortcut functions to the cipher's object interface.
94 *
95 * @example
96 *
97 * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
98 * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
99 */
100 C.RC4 = StreamCipher._createHelper(RC4);
101
102 /**
103 * Modified RC4 stream cipher algorithm.
104 */
105 var RC4Drop = C_algo.RC4Drop = RC4.extend({
106 /**
107 * Configuration options.
108 *
109 * @property {number} drop The number of keystream words to drop. Default 192
110 */
111 cfg: RC4.cfg.extend({
112 drop: 192
113 }),
114
115 _doReset: function () {
116 RC4._doReset.call(this);
117
118 // Drop
119 for (var i = this.cfg.drop; i > 0; i--) {
120 generateKeystreamWord.call(this);
121 }
122 }
123 });
124
125 /**
126 * Shortcut functions to the cipher's object interface.
127 *
128 * @example
129 *
130 * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
131 * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
132 */
133 C.RC4Drop = StreamCipher._createHelper(RC4Drop);
134 }());
135
136
137 return CryptoJS.RC4;
138
139}));
\No newline at end of file