1
+ ( function ( ) {
2
+ var crypt = require ( 'crypt' ) ,
3
+ utf8 = require ( 'charenc' ) . utf8 ,
4
+ isBuffer = require ( 'is-buffer' ) ,
5
+ bin = require ( 'charenc' ) . bin ,
6
+
7
+ // The core
8
+ md5 = function ( message , options ) {
9
+ // Convert to byte array
10
+ if ( message . constructor == String )
11
+ if ( options && options . encoding === 'binary' )
12
+ message = bin . stringToBytes ( message ) ;
13
+ else
14
+ message = utf8 . stringToBytes ( message ) ;
15
+ else if ( isBuffer ( message ) )
16
+ message = Array . prototype . slice . call ( message , 0 ) ;
17
+ else if ( ! Array . isArray ( message ) && message . constructor !== Uint8Array )
18
+ message = message . toString ( ) ;
19
+ // else, assume byte array already
20
+
21
+ var m = crypt . bytesToWords ( message ) ,
22
+ l = message . length * 8 ,
23
+ a = 1732584193 ,
24
+ b = - 271733879 ,
25
+ c = - 1732584194 ,
26
+ d = 271733878 ;
27
+
28
+ // Swap endian
29
+ for ( var i = 0 ; i < m . length ; i ++ ) {
30
+ m [ i ] = ( ( m [ i ] << 8 ) | ( m [ i ] >>> 24 ) ) & 0x00FF00FF |
31
+ ( ( m [ i ] << 24 ) | ( m [ i ] >>> 8 ) ) & 0xFF00FF00 ;
32
+ }
33
+
34
+ // Padding
35
+ m [ l >>> 5 ] |= 0x80 << ( l % 32 ) ;
36
+ m [ ( ( ( l + 64 ) >>> 9 ) << 4 ) + 14 ] = l ;
37
+
38
+ // Method shortcuts
39
+ var FF = md5 . _ff ,
40
+ GG = md5 . _gg ,
41
+ HH = md5 . _hh ,
42
+ II = md5 . _ii ;
43
+
44
+ for ( var i = 0 ; i < m . length ; i += 16 ) {
45
+
46
+ var aa = a ,
47
+ bb = b ,
48
+ cc = c ,
49
+ dd = d ;
50
+
51
+ a = FF ( a , b , c , d , m [ i + 0 ] , 7 , - 680876936 ) ;
52
+ d = FF ( d , a , b , c , m [ i + 1 ] , 12 , - 389564586 ) ;
53
+ c = FF ( c , d , a , b , m [ i + 2 ] , 17 , 606105819 ) ;
54
+ b = FF ( b , c , d , a , m [ i + 3 ] , 22 , - 1044525330 ) ;
55
+ a = FF ( a , b , c , d , m [ i + 4 ] , 7 , - 176418897 ) ;
56
+ d = FF ( d , a , b , c , m [ i + 5 ] , 12 , 1200080426 ) ;
57
+ c = FF ( c , d , a , b , m [ i + 6 ] , 17 , - 1473231341 ) ;
58
+ b = FF ( b , c , d , a , m [ i + 7 ] , 22 , - 45705983 ) ;
59
+ a = FF ( a , b , c , d , m [ i + 8 ] , 7 , 1770035416 ) ;
60
+ d = FF ( d , a , b , c , m [ i + 9 ] , 12 , - 1958414417 ) ;
61
+ c = FF ( c , d , a , b , m [ i + 10 ] , 17 , - 42063 ) ;
62
+ b = FF ( b , c , d , a , m [ i + 11 ] , 22 , - 1990404162 ) ;
63
+ a = FF ( a , b , c , d , m [ i + 12 ] , 7 , 1804603682 ) ;
64
+ d = FF ( d , a , b , c , m [ i + 13 ] , 12 , - 40341101 ) ;
65
+ c = FF ( c , d , a , b , m [ i + 14 ] , 17 , - 1502002290 ) ;
66
+ b = FF ( b , c , d , a , m [ i + 15 ] , 22 , 1236535329 ) ;
67
+
68
+ a = GG ( a , b , c , d , m [ i + 1 ] , 5 , - 165796510 ) ;
69
+ d = GG ( d , a , b , c , m [ i + 6 ] , 9 , - 1069501632 ) ;
70
+ c = GG ( c , d , a , b , m [ i + 11 ] , 14 , 643717713 ) ;
71
+ b = GG ( b , c , d , a , m [ i + 0 ] , 20 , - 373897302 ) ;
72
+ a = GG ( a , b , c , d , m [ i + 5 ] , 5 , - 701558691 ) ;
73
+ d = GG ( d , a , b , c , m [ i + 10 ] , 9 , 38016083 ) ;
74
+ c = GG ( c , d , a , b , m [ i + 15 ] , 14 , - 660478335 ) ;
75
+ b = GG ( b , c , d , a , m [ i + 4 ] , 20 , - 405537848 ) ;
76
+ a = GG ( a , b , c , d , m [ i + 9 ] , 5 , 568446438 ) ;
77
+ d = GG ( d , a , b , c , m [ i + 14 ] , 9 , - 1019803690 ) ;
78
+ c = GG ( c , d , a , b , m [ i + 3 ] , 14 , - 187363961 ) ;
79
+ b = GG ( b , c , d , a , m [ i + 8 ] , 20 , 1163531501 ) ;
80
+ a = GG ( a , b , c , d , m [ i + 13 ] , 5 , - 1444681467 ) ;
81
+ d = GG ( d , a , b , c , m [ i + 2 ] , 9 , - 51403784 ) ;
82
+ c = GG ( c , d , a , b , m [ i + 7 ] , 14 , 1735328473 ) ;
83
+ b = GG ( b , c , d , a , m [ i + 12 ] , 20 , - 1926607734 ) ;
84
+
85
+ a = HH ( a , b , c , d , m [ i + 5 ] , 4 , - 378558 ) ;
86
+ d = HH ( d , a , b , c , m [ i + 8 ] , 11 , - 2022574463 ) ;
87
+ c = HH ( c , d , a , b , m [ i + 11 ] , 16 , 1839030562 ) ;
88
+ b = HH ( b , c , d , a , m [ i + 14 ] , 23 , - 35309556 ) ;
89
+ a = HH ( a , b , c , d , m [ i + 1 ] , 4 , - 1530992060 ) ;
90
+ d = HH ( d , a , b , c , m [ i + 4 ] , 11 , 1272893353 ) ;
91
+ c = HH ( c , d , a , b , m [ i + 7 ] , 16 , - 155497632 ) ;
92
+ b = HH ( b , c , d , a , m [ i + 10 ] , 23 , - 1094730640 ) ;
93
+ a = HH ( a , b , c , d , m [ i + 13 ] , 4 , 681279174 ) ;
94
+ d = HH ( d , a , b , c , m [ i + 0 ] , 11 , - 358537222 ) ;
95
+ c = HH ( c , d , a , b , m [ i + 3 ] , 16 , - 722521979 ) ;
96
+ b = HH ( b , c , d , a , m [ i + 6 ] , 23 , 76029189 ) ;
97
+ a = HH ( a , b , c , d , m [ i + 9 ] , 4 , - 640364487 ) ;
98
+ d = HH ( d , a , b , c , m [ i + 12 ] , 11 , - 421815835 ) ;
99
+ c = HH ( c , d , a , b , m [ i + 15 ] , 16 , 530742520 ) ;
100
+ b = HH ( b , c , d , a , m [ i + 2 ] , 23 , - 995338651 ) ;
101
+
102
+ a = II ( a , b , c , d , m [ i + 0 ] , 6 , - 198630844 ) ;
103
+ d = II ( d , a , b , c , m [ i + 7 ] , 10 , 1126891415 ) ;
104
+ c = II ( c , d , a , b , m [ i + 14 ] , 15 , - 1416354905 ) ;
105
+ b = II ( b , c , d , a , m [ i + 5 ] , 21 , - 57434055 ) ;
106
+ a = II ( a , b , c , d , m [ i + 12 ] , 6 , 1700485571 ) ;
107
+ d = II ( d , a , b , c , m [ i + 3 ] , 10 , - 1894986606 ) ;
108
+ c = II ( c , d , a , b , m [ i + 10 ] , 15 , - 1051523 ) ;
109
+ b = II ( b , c , d , a , m [ i + 1 ] , 21 , - 2054922799 ) ;
110
+ a = II ( a , b , c , d , m [ i + 8 ] , 6 , 1873313359 ) ;
111
+ d = II ( d , a , b , c , m [ i + 15 ] , 10 , - 30611744 ) ;
112
+ c = II ( c , d , a , b , m [ i + 6 ] , 15 , - 1560198380 ) ;
113
+ b = II ( b , c , d , a , m [ i + 13 ] , 21 , 1309151649 ) ;
114
+ a = II ( a , b , c , d , m [ i + 4 ] , 6 , - 145523070 ) ;
115
+ d = II ( d , a , b , c , m [ i + 11 ] , 10 , - 1120210379 ) ;
116
+ c = II ( c , d , a , b , m [ i + 2 ] , 15 , 718787259 ) ;
117
+ b = II ( b , c , d , a , m [ i + 9 ] , 21 , - 343485551 ) ;
118
+
119
+ a = ( a + aa ) >>> 0 ;
120
+ b = ( b + bb ) >>> 0 ;
121
+ c = ( c + cc ) >>> 0 ;
122
+ d = ( d + dd ) >>> 0 ;
123
+ }
124
+
125
+ return crypt . endian ( [ a , b , c , d ] ) ;
126
+ } ;
127
+
128
+ // Auxiliary functions
129
+ md5 . _ff = function ( a , b , c , d , x , s , t ) {
130
+ var n = a + ( b & c | ~ b & d ) + ( x >>> 0 ) + t ;
131
+ return ( ( n << s ) | ( n >>> ( 32 - s ) ) ) + b ;
132
+ } ;
133
+ md5 . _gg = function ( a , b , c , d , x , s , t ) {
134
+ var n = a + ( b & d | c & ~ d ) + ( x >>> 0 ) + t ;
135
+ return ( ( n << s ) | ( n >>> ( 32 - s ) ) ) + b ;
136
+ } ;
137
+ md5 . _hh = function ( a , b , c , d , x , s , t ) {
138
+ var n = a + ( b ^ c ^ d ) + ( x >>> 0 ) + t ;
139
+ return ( ( n << s ) | ( n >>> ( 32 - s ) ) ) + b ;
140
+ } ;
141
+ md5 . _ii = function ( a , b , c , d , x , s , t ) {
142
+ var n = a + ( c ^ ( b | ~ d ) ) + ( x >>> 0 ) + t ;
143
+ return ( ( n << s ) | ( n >>> ( 32 - s ) ) ) + b ;
144
+ } ;
145
+
146
+ // Package private blocksize
147
+ md5 . _blocksize = 16 ;
148
+ md5 . _digestsize = 16 ;
149
+
150
+ module . exports = function ( message , options ) {
151
+ if ( message === undefined || message === null )
152
+ throw new Error ( 'Illegal argument ' + message ) ;
153
+
154
+ var digestbytes = crypt . wordsToBytes ( md5 ( message , options ) ) ;
155
+ return options && options . asBytes ? digestbytes :
156
+ options && options . asString ? bin . bytesToString ( digestbytes ) :
157
+ crypt . bytesToHex ( digestbytes ) ;
158
+ } ;
159
+
160
+ } ) ( ) ;
0 commit comments