tizen beta release
[profile/ivi/webkit-efl.git] / LayoutTests / fast / js / kde / script-tests / md5-2.js
1 shouldBe("hexMD5('kde')", "'186cf28b76f2264e9fea8fcf91cb4f5d'");
2
3 /*
4  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
5  * Digest Algorithm, as defined in RFC 1321.
6  * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002.
7  * Code also contributed by Greg Holt
8  * See http://pajhome.org.uk/site/legal.html for details.
9  */
10
11 /*
12  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
13  * to work around bugs in some JS interpreters.
14  */
15 function safe_add(x, y)
16 {
17   var lsw = (x & 0xFFFF) + (y & 0xFFFF)
18   var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
19   return (msw << 16) | (lsw & 0xFFFF)
20 }
21
22 /*
23  * Bitwise rotate a 32-bit number to the left.
24  */
25 function rol(num, cnt)
26 {
27   return (num << cnt) | (num >>> (32 - cnt))
28 }
29
30 /*
31  * These functions implement the four basic operations the algorithm uses.
32  */
33 function cmn(q, a, b, x, s, t)
34 {
35   return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
36 }
37 function ff(a, b, c, d, x, s, t)
38 {
39   return cmn((b & c) | ((~b) & d), a, b, x, s, t)
40 }
41 function gg(a, b, c, d, x, s, t)
42 {
43   return cmn((b & d) | (c & (~d)), a, b, x, s, t)
44 }
45 function hh(a, b, c, d, x, s, t)
46 {
47   return cmn(b ^ c ^ d, a, b, x, s, t)
48 }
49 function ii(a, b, c, d, x, s, t)
50 {
51   return cmn(c ^ (b | (~d)), a, b, x, s, t)
52 }
53
54 /*
55  * Calculate the MD5 of an array of little-endian words, producing an array
56  * of little-endian words.
57  */
58 function coreMD5(x)
59 {
60   var a =  1732584193
61   var b = -271733879
62   var c = -1732584194
63   var d =  271733878
64
65   for(i = 0; i < x.length; i += 16)
66   {
67     var olda = a
68     var oldb = b
69     var oldc = c
70     var oldd = d
71
72     a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)
73     d = ff(d, a, b, c, x[i+ 1], 12, -389564586)
74     c = ff(c, d, a, b, x[i+ 2], 17,  606105819)
75     b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)
76     a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)
77     d = ff(d, a, b, c, x[i+ 5], 12,  1200080426)
78     c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)
79     b = ff(b, c, d, a, x[i+ 7], 22, -45705983)
80     a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416)
81     d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)
82     c = ff(c, d, a, b, x[i+10], 17, -42063)
83     b = ff(b, c, d, a, x[i+11], 22, -1990404162)
84     a = ff(a, b, c, d, x[i+12], 7 ,  1804603682)
85     d = ff(d, a, b, c, x[i+13], 12, -40341101)
86     c = ff(c, d, a, b, x[i+14], 17, -1502002290)
87     b = ff(b, c, d, a, x[i+15], 22,  1236535329)
88
89     a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)
90     d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)
91     c = gg(c, d, a, b, x[i+11], 14,  643717713)
92     b = gg(b, c, d, a, x[i+ 0], 20, -373897302)
93     a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)
94     d = gg(d, a, b, c, x[i+10], 9 ,  38016083)
95     c = gg(c, d, a, b, x[i+15], 14, -660478335)
96     b = gg(b, c, d, a, x[i+ 4], 20, -405537848)
97     a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438)
98     d = gg(d, a, b, c, x[i+14], 9 , -1019803690)
99     c = gg(c, d, a, b, x[i+ 3], 14, -187363961)
100     b = gg(b, c, d, a, x[i+ 8], 20,  1163531501)
101     a = gg(a, b, c, d, x[i+13], 5 , -1444681467)
102     d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)
103     c = gg(c, d, a, b, x[i+ 7], 14,  1735328473)
104     b = gg(b, c, d, a, x[i+12], 20, -1926607734)
105
106     a = hh(a, b, c, d, x[i+ 5], 4 , -378558)
107     d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)
108     c = hh(c, d, a, b, x[i+11], 16,  1839030562)
109     b = hh(b, c, d, a, x[i+14], 23, -35309556)
110     a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)
111     d = hh(d, a, b, c, x[i+ 4], 11,  1272893353)
112     c = hh(c, d, a, b, x[i+ 7], 16, -155497632)
113     b = hh(b, c, d, a, x[i+10], 23, -1094730640)
114     a = hh(a, b, c, d, x[i+13], 4 ,  681279174)
115     d = hh(d, a, b, c, x[i+ 0], 11, -358537222)
116     c = hh(c, d, a, b, x[i+ 3], 16, -722521979)
117     b = hh(b, c, d, a, x[i+ 6], 23,  76029189)
118     a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)
119     d = hh(d, a, b, c, x[i+12], 11, -421815835)
120     c = hh(c, d, a, b, x[i+15], 16,  530742520)
121     b = hh(b, c, d, a, x[i+ 2], 23, -995338651)
122
123     a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)
124     d = ii(d, a, b, c, x[i+ 7], 10,  1126891415)
125     c = ii(c, d, a, b, x[i+14], 15, -1416354905)
126     b = ii(b, c, d, a, x[i+ 5], 21, -57434055)
127     a = ii(a, b, c, d, x[i+12], 6 ,  1700485571)
128     d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)
129     c = ii(c, d, a, b, x[i+10], 15, -1051523)
130     b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)
131     a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359)
132     d = ii(d, a, b, c, x[i+15], 10, -30611744)
133     c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)
134     b = ii(b, c, d, a, x[i+13], 21,  1309151649)
135     a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)
136     d = ii(d, a, b, c, x[i+11], 10, -1120210379)
137     c = ii(c, d, a, b, x[i+ 2], 15,  718787259)
138     b = ii(b, c, d, a, x[i+ 9], 21, -343485551)
139
140     a = safe_add(a, olda)
141     b = safe_add(b, oldb)
142     c = safe_add(c, oldc)
143     d = safe_add(d, oldd)
144   }
145   return [a, b, c, d]
146 }
147
148 /*
149  * Convert an array of little-endian words to a hex string.
150  */
151 function binl2hex(binarray)
152 {
153   var hex_tab = "0123456789abcdef"
154   var str = ""
155   for(var i = 0; i < binarray.length * 4; i++)
156   {
157     str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
158            hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)
159   }
160   return str
161 }
162
163 /*
164  * Convert an array of little-endian words to a base64 encoded string.
165  */
166 function binl2b64(binarray)
167 {
168   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
169   var str = ""
170   for(var i = 0; i < binarray.length * 32; i += 6)
171   {
172     str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |
173                       ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))
174   }
175   return str
176 }
177
178 /*
179  * Convert an 8-bit character string to a sequence of 16-word blocks, stored
180  * as an array, and append appropriate padding for MD4/5 calculation.
181  * If any of the characters are >255, the high byte is silently ignored.
182  */
183 function str2binl(str)
184 {
185   var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks
186   var blks = new Array(nblk * 16)
187   for(var i = 0; i < nblk * 16; i++) blks[i] = 0
188   for(var i = 0; i < str.length; i++)
189     blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)
190   blks[i>>2] |= 0x80 << ((i%4) * 8)
191   blks[nblk*16-2] = str.length * 8
192   return blks
193 }
194
195 /*
196  * Convert a wide-character string to a sequence of 16-word blocks, stored as
197  * an array, and append appropriate padding for MD4/5 calculation.
198  */
199 function strw2binl(str)
200 {
201   var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks
202   var blks = new Array(nblk * 16)
203   for(var i = 0; i < nblk * 16; i++) blks[i] = 0
204   for(var i = 0; i < str.length; i++)
205     blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)
206   blks[i>>1] |= 0x80 << ((i%2) * 16)
207   blks[nblk*16-2] = str.length * 16
208   return blks
209 }
210
211 /*
212  * External interface
213  */
214 function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }
215 function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
216 function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }
217 function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
218 /* Backward compatibility */
219 function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }