7
7
* See http://pajhome.org.uk/crypt/md5 for more info.
8
8
*/
9
9
10
- /*
11
- * Configurable variables. You may need to tweak these to be compatible with
12
- * the server-side, but the defaults work in most cases.
13
- */
14
- var hexcase = 0 ; /* hex output format. 0 - lowercase; 1 - uppercase */
15
- var b64pad = "=" ; /* base-64 pad character. "=" for strict RFC compliance */
16
- var chrsz = 8 ; /* bits per input character. 8 - ASCII; 16 - Unicode */
17
-
18
- /*
19
- * These are the functions you'll usually want to call
20
- * They take string arguments and return either hex or base-64 encoded strings
21
- */
22
- function hex_md5 ( s ) { return binl2hex ( core_md5 ( str2binl ( s ) , s . length * chrsz ) ) ; }
23
- function b64_md5 ( s ) { return binl2b64 ( core_md5 ( str2binl ( s ) , s . length * chrsz ) ) ; }
24
- function str_md5 ( s ) { return binl2str ( core_md5 ( str2binl ( s ) , s . length * chrsz ) ) ; }
25
- function hex_hmac_md5 ( key , data ) { return binl2hex ( core_hmac_md5 ( key , data ) ) ; }
26
- function b64_hmac_md5 ( key , data ) { return binl2b64 ( core_hmac_md5 ( key , data ) ) ; }
27
- function str_hmac_md5 ( key , data ) { return binl2str ( core_hmac_md5 ( key , data ) ) ; }
10
+ var helpers = require ( './helpers' ) ;
28
11
29
12
/*
30
13
* Perform a simple self-test to see if the VM is working
@@ -156,25 +139,6 @@ function md5_ii(a, b, c, d, x, s, t)
156
139
return md5_cmn ( c ^ ( b | ( ~ d ) ) , a , b , x , s , t ) ;
157
140
}
158
141
159
- /*
160
- * Calculate the HMAC-MD5, of a key and some data
161
- */
162
- function core_hmac_md5 ( key , data )
163
- {
164
- var bkey = str2binl ( key ) ;
165
- if ( bkey . length > 16 ) bkey = core_md5 ( bkey , key . length * chrsz ) ;
166
-
167
- var ipad = Array ( 16 ) , opad = Array ( 16 ) ;
168
- for ( var i = 0 ; i < 16 ; i ++ )
169
- {
170
- ipad [ i ] = bkey [ i ] ^ 0x36363636 ;
171
- opad [ i ] = bkey [ i ] ^ 0x5C5C5C5C ;
172
- }
173
-
174
- var hash = core_md5 ( ipad . concat ( str2binl ( data ) ) , 512 + data . length * chrsz ) ;
175
- return core_md5 ( opad . concat ( hash ) , 512 + 128 ) ;
176
- }
177
-
178
142
/*
179
143
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
180
144
* to work around bugs in some JS interpreters.
@@ -194,70 +158,6 @@ function bit_rol(num, cnt)
194
158
return ( num << cnt ) | ( num >>> ( 32 - cnt ) ) ;
195
159
}
196
160
197
- /*
198
- * Convert a string to an array of little-endian words
199
- * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
200
- */
201
- function str2binl ( str )
202
- {
203
- var bin = Array ( ) ;
204
- var mask = ( 1 << chrsz ) - 1 ;
205
- for ( var i = 0 ; i < str . length * chrsz ; i += chrsz )
206
- bin [ i >> 5 ] |= ( str . charCodeAt ( i / chrsz ) & mask ) << ( i % 32 ) ;
207
- return bin ;
208
- }
209
-
210
- /*
211
- * Convert an array of little-endian words to a string
212
- */
213
- function binl2str ( bin )
214
- {
215
- var str = "" ;
216
- var mask = ( 1 << chrsz ) - 1 ;
217
- for ( var i = 0 ; i < bin . length * 32 ; i += chrsz )
218
- str += String . fromCharCode ( ( bin [ i >> 5 ] >>> ( i % 32 ) ) & mask ) ;
219
- return str ;
220
- }
221
-
222
- /*
223
- * Convert an array of little-endian words to a hex string.
224
- */
225
- function binl2hex ( binarray )
226
- {
227
- var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef" ;
228
- var str = "" ;
229
- for ( var i = 0 ; i < binarray . length * 4 ; i ++ )
230
- {
231
- str += hex_tab . charAt ( ( binarray [ i >> 2 ] >> ( ( i % 4 ) * 8 + 4 ) ) & 0xF ) +
232
- hex_tab . charAt ( ( binarray [ i >> 2 ] >> ( ( i % 4 ) * 8 ) ) & 0xF ) ;
233
- }
234
- return str ;
235
- }
236
-
237
- /*
238
- * Convert an array of little-endian words to a base-64 string
239
- */
240
- function binl2b64 ( binarray )
241
- {
242
- var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
243
- var str = "" ;
244
- for ( var i = 0 ; i < binarray . length * 4 ; i += 3 )
245
- {
246
- var triplet = ( ( ( binarray [ i >> 2 ] >> 8 * ( i % 4 ) ) & 0xFF ) << 16 )
247
- | ( ( ( binarray [ i + 1 >> 2 ] >> 8 * ( ( i + 1 ) % 4 ) ) & 0xFF ) << 8 )
248
- | ( ( binarray [ i + 2 >> 2 ] >> 8 * ( ( i + 2 ) % 4 ) ) & 0xFF ) ;
249
- for ( var j = 0 ; j < 4 ; j ++ )
250
- {
251
- if ( i * 8 + j * 6 > binarray . length * 32 ) str += b64pad ;
252
- else str += tab . charAt ( ( triplet >> 6 * ( 3 - j ) ) & 0x3F ) ;
253
- }
254
- }
255
- return str ;
256
- }
257
-
258
- exports . hex_md5 = hex_md5 ;
259
- exports . b64_md5 = b64_md5 ;
260
- exports . bin_md5 = str_md5 ;
261
- exports . hex_hmac_md5 = hex_hmac_md5 ;
262
- exports . b64_hmac_md5 = b64_hmac_md5 ;
263
- exports . bin_hmac_md5 = str_hmac_md5 ;
161
+ module . exports = function md5 ( buf ) {
162
+ return helpers . hash ( buf , core_md5 , 16 ) ;
163
+ } ;
0 commit comments