UNPKG

9.39 kBJavaScriptView Raw
1;(function (root, factory) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 /** @preserve
17 (c) 2012 by Cédric Mesnil. All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
20
21 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
22 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 (function (Math) {
28 // Shortcuts
29 var C = CryptoJS;
30 var C_lib = C.lib;
31 var WordArray = C_lib.WordArray;
32 var Hasher = C_lib.Hasher;
33 var C_algo = C.algo;
34
35 // Constants table
36 var _zl = WordArray.create([
37 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
38 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
39 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
40 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
41 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]);
42 var _zr = WordArray.create([
43 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
44 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
45 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
46 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
47 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]);
48 var _sl = WordArray.create([
49 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
50 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
51 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
52 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
53 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 ]);
54 var _sr = WordArray.create([
55 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
56 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
57 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
58 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
59 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 ]);
60
61 var _hl = WordArray.create([ 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]);
62 var _hr = WordArray.create([ 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]);
63
64 /**
65 * RIPEMD160 hash algorithm.
66 */
67 var RIPEMD160 = C_algo.RIPEMD160 = Hasher.extend({
68 _doReset: function () {
69 this._hash = WordArray.create([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]);
70 },
71
72 _doProcessBlock: function (M, offset) {
73
74 // Swap endian
75 for (var i = 0; i < 16; i++) {
76 // Shortcuts
77 var offset_i = offset + i;
78 var M_offset_i = M[offset_i];
79
80 // Swap
81 M[offset_i] = (
82 (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
83 (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
84 );
85 }
86 // Shortcut
87 var H = this._hash.words;
88 var hl = _hl.words;
89 var hr = _hr.words;
90 var zl = _zl.words;
91 var zr = _zr.words;
92 var sl = _sl.words;
93 var sr = _sr.words;
94
95 // Working variables
96 var al, bl, cl, dl, el;
97 var ar, br, cr, dr, er;
98
99 ar = al = H[0];
100 br = bl = H[1];
101 cr = cl = H[2];
102 dr = dl = H[3];
103 er = el = H[4];
104 // Computation
105 var t;
106 for (var i = 0; i < 80; i += 1) {
107 t = (al + M[offset+zl[i]])|0;
108 if (i<16){
109 t += f1(bl,cl,dl) + hl[0];
110 } else if (i<32) {
111 t += f2(bl,cl,dl) + hl[1];
112 } else if (i<48) {
113 t += f3(bl,cl,dl) + hl[2];
114 } else if (i<64) {
115 t += f4(bl,cl,dl) + hl[3];
116 } else {// if (i<80) {
117 t += f5(bl,cl,dl) + hl[4];
118 }
119 t = t|0;
120 t = rotl(t,sl[i]);
121 t = (t+el)|0;
122 al = el;
123 el = dl;
124 dl = rotl(cl, 10);
125 cl = bl;
126 bl = t;
127
128 t = (ar + M[offset+zr[i]])|0;
129 if (i<16){
130 t += f5(br,cr,dr) + hr[0];
131 } else if (i<32) {
132 t += f4(br,cr,dr) + hr[1];
133 } else if (i<48) {
134 t += f3(br,cr,dr) + hr[2];
135 } else if (i<64) {
136 t += f2(br,cr,dr) + hr[3];
137 } else {// if (i<80) {
138 t += f1(br,cr,dr) + hr[4];
139 }
140 t = t|0;
141 t = rotl(t,sr[i]) ;
142 t = (t+er)|0;
143 ar = er;
144 er = dr;
145 dr = rotl(cr, 10);
146 cr = br;
147 br = t;
148 }
149 // Intermediate hash value
150 t = (H[1] + cl + dr)|0;
151 H[1] = (H[2] + dl + er)|0;
152 H[2] = (H[3] + el + ar)|0;
153 H[3] = (H[4] + al + br)|0;
154 H[4] = (H[0] + bl + cr)|0;
155 H[0] = t;
156 },
157
158 _doFinalize: function () {
159 // Shortcuts
160 var data = this._data;
161 var dataWords = data.words;
162
163 var nBitsTotal = this._nDataBytes * 8;
164 var nBitsLeft = data.sigBytes * 8;
165
166 // Add padding
167 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
168 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
169 (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
170 (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
171 );
172 data.sigBytes = (dataWords.length + 1) * 4;
173
174 // Hash final blocks
175 this._process();
176
177 // Shortcuts
178 var hash = this._hash;
179 var H = hash.words;
180
181 // Swap endian
182 for (var i = 0; i < 5; i++) {
183 // Shortcut
184 var H_i = H[i];
185
186 // Swap
187 H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
188 (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
189 }
190
191 // Return final computed hash
192 return hash;
193 },
194
195 clone: function () {
196 var clone = Hasher.clone.call(this);
197 clone._hash = this._hash.clone();
198
199 return clone;
200 }
201 });
202
203
204 function f1(x, y, z) {
205 return ((x) ^ (y) ^ (z));
206
207 }
208
209 function f2(x, y, z) {
210 return (((x)&(y)) | ((~x)&(z)));
211 }
212
213 function f3(x, y, z) {
214 return (((x) | (~(y))) ^ (z));
215 }
216
217 function f4(x, y, z) {
218 return (((x) & (z)) | ((y)&(~(z))));
219 }
220
221 function f5(x, y, z) {
222 return ((x) ^ ((y) |(~(z))));
223
224 }
225
226 function rotl(x,n) {
227 return (x<<n) | (x>>>(32-n));
228 }
229
230
231 /**
232 * Shortcut function to the hasher's object interface.
233 *
234 * @param {WordArray|string} message The message to hash.
235 *
236 * @return {WordArray} The hash.
237 *
238 * @static
239 *
240 * @example
241 *
242 * var hash = CryptoJS.RIPEMD160('message');
243 * var hash = CryptoJS.RIPEMD160(wordArray);
244 */
245 C.RIPEMD160 = Hasher._createHelper(RIPEMD160);
246
247 /**
248 * Shortcut function to the HMAC's object interface.
249 *
250 * @param {WordArray|string} message The message to hash.
251 * @param {WordArray|string} key The secret key.
252 *
253 * @return {WordArray} The HMAC.
254 *
255 * @static
256 *
257 * @example
258 *
259 * var hmac = CryptoJS.HmacRIPEMD160(message, key);
260 */
261 C.HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160);
262 }(Math));
263
264
265 return CryptoJS.RIPEMD160;
266
267}));
\No newline at end of file