corrade-nucleus-nucleons – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 (function(){
2  
3 var C = (typeof window === 'undefined') ? require('./Crypto').Crypto : window.Crypto;
4  
5 // Shortcuts
6 var util = C.util,
7 charenc = C.charenc,
8 UTF8 = charenc.UTF8,
9 Binary = charenc.Binary;
10  
11 var MARC4 = C.MARC4 = {
12  
13 /**
14 * Public API
15 */
16  
17 encrypt: function (message, password) {
18  
19 var
20  
21 // Convert to bytes
22 m = UTF8.stringToBytes(message),
23  
24 // Generate random IV
25 iv = util.randomBytes(16),
26  
27 // Generate key
28 k = password.constructor == String ?
29 // Derive key from passphrase
30 C.PBKDF2(password, iv, 32, { asBytes: true }) :
31 // else, assume byte array representing cryptographic key
32 password;
33  
34 // Encrypt
35 MARC4._marc4(m, k, 1536);
36  
37 // Return ciphertext
38 return util.bytesToBase64(iv.concat(m));
39  
40 },
41  
42 decrypt: function (ciphertext, password) {
43  
44 var
45  
46 // Convert to bytes
47 c = util.base64ToBytes(ciphertext),
48  
49 // Separate IV and message
50 iv = c.splice(0, 16),
51  
52 // Generate key
53 k = password.constructor == String ?
54 // Derive key from passphrase
55 C.PBKDF2(password, iv, 32, { asBytes: true }) :
56 // else, assume byte array representing cryptographic key
57 password;
58  
59 // Decrypt
60 MARC4._marc4(c, k, 1536);
61  
62 // Return plaintext
63 return UTF8.bytesToString(c);
64  
65 },
66  
67  
68 /**
69 * Internal methods
70 */
71  
72 // The core
73 _marc4: function (m, k, drop) {
74  
75 // State variables
76 var i, j, s, temp;
77  
78 // Key setup
79 for (i = 0, s = []; i < 256; i++) s[i] = i;
80 for (i = 0, j = 0; i < 256; i++) {
81  
82 j = (j + s[i] + k[i % k.length]) % 256;
83  
84 // Swap
85 temp = s[i];
86 s[i] = s[j];
87 s[j] = temp;
88  
89 }
90  
91 // Clear counters
92 i = j = 0;
93  
94 // Encryption
95 for (var k = -drop; k < m.length; k++) {
96  
97 i = (i + 1) % 256;
98 j = (j + s[i]) % 256;
99  
100 // Swap
101 temp = s[i];
102 s[i] = s[j];
103 s[j] = temp;
104  
105 // Stop here if we're still dropping keystream
106 if (k < 0) continue;
107  
108 // Encrypt
109 m[k] ^= s[(s[i] + s[j]) % 256];
110  
111 }
112  
113 }
114  
115 };
116  
117 })();