1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/tizen/hashes/rmd160.js Wed Jul 31 19:48:00 2013 +0200 1.3 @@ -0,0 +1,249 @@ 1.4 +/* A JavaScript implementation of RIPEMD-160, as specified at 1.5 + * 1.6 + * http://www.esat.kuleuven.ac.be/~cosicart/pdf/AB-9601/ 1.7 + * 1.8 + * This is pretty much a straight translation of the pseudocode, which is 1.9 + * shorter than the reference version which has loops unrolled, but is 1.10 + * also somewhat slower. 1.11 + * 1.12 + * More information about RIPEMD-160 can be found at 1.13 + * 1.14 + * http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html 1.15 + * 1.16 + * Copyright (c) 2004, Jeremy Lin. Written 2004/02/29. 1.17 + * 1.18 + * You may use this code under the terms of the BSD-style jsotp license: 1.19 + * 1.20 + * http://www.ocf.berkeley.edu/~jjlin/jsotp/license.html 1.21 + * 1.22 + * The utility functions and general framework are borrowed from Paul Johnston's 1.23 + * MD4/MD5/SHA-1 JavaScript implementations (http://pajhome.org.uk/crypt/md5), so 1.24 + * 1.25 + * Portions copyright (c) 1999-2002, Paul Johnston. 1.26 + */ 1.27 + 1.28 +/* 1.29 + * Configurable variables. You may need to tweak these to be compatible with 1.30 + * the server-side, but the defaults work in most cases. 1.31 + */ 1.32 +var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ 1.33 +var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ 1.34 +var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ 1.35 + 1.36 +/* 1.37 + * These are the functions you'll usually want to call 1.38 + * They take string arguments and return either hex or base-64 encoded strings 1.39 + */ 1.40 +function hexrmd160(s){return binl2hex(corermd160(str2binl(s),s.length * chrsz));} 1.41 +function b64rmd160(s){return binl2b64(corermd160(str2binl(s),s.length * chrsz));} 1.42 +function strrmd160(s){return binl2str(corermd160(str2binl(s),s.length * chrsz));} 1.43 +function hexhmacrmd160(key, data){ return binl2hex(corehmacrmd160(key, data));} 1.44 +function b64hmacrmd160(key, data){ return binl2b64(corehmacrmd160(key, data));} 1.45 +function strhmacrmd160(key, data){ return binl2str(corehmacrmd160(key, data));} 1.46 + 1.47 +/* 1.48 + * Perform a simple self-test to see if the VM is working 1.49 + */ 1.50 +function rmd160vmtest() 1.51 +{ 1.52 + return hexrmd160("abc") == "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"; 1.53 +} 1.54 + 1.55 +/* 1.56 + * Calculate the RMD-160 of an array of big-endian words, and a bit length 1.57 + */ 1.58 +function corermd160(x, len) 1.59 +{ 1.60 + /* append padding */ 1.61 + x[len >> 5] |= 0x80 << (len % 32); 1.62 + x[(((len + 64) >>> 9) << 4) + 14] = len; 1.63 + 1.64 + var h0 = 0x67452301; 1.65 + var h1 = 0xefcdab89; 1.66 + var h2 = 0x98badcfe; 1.67 + var h3 = 0x10325476; 1.68 + var h4 = 0xc3d2e1f0; 1.69 + 1.70 + for (var i = 0; i < x.length; i += 16) { 1.71 + var T; 1.72 + var A1 = h0, B1 = h1, C1 = h2, D1 = h3, E1 = h4; 1.73 + var A2 = h0, B2 = h1, C2 = h2, D2 = h3, E2 = h4; 1.74 + for (var j = 0; j <= 79; ++j) { 1.75 + T = safeadd(A1, rmd160f(j, B1, C1, D1)); 1.76 + T = safeadd(T, x[i + rmd160r1[j]]); 1.77 + T = safeadd(T, rmd160K1(j)); 1.78 + T = safeadd(rol(T, rmd160s1[j]), E1); 1.79 + A1 = E1; E1 = D1; D1 = rol(C1, 10); C1 = B1; B1 = T; 1.80 + T = safeadd(A2, rmd160f(79-j, B2, C2, D2)); 1.81 + T = safeadd(T, x[i + rmd160r2[j]]); 1.82 + T = safeadd(T, rmd160K2(j)); 1.83 + T = safeadd(rol(T, rmd160s2[j]), E2); 1.84 + A2 = E2; E2 = D2; D2 = rol(C2, 10); C2 = B2; B2 = T; 1.85 + } 1.86 + T = safeadd(h1, safeadd(C1, D2)); 1.87 + h1 = safeadd(h2, safeadd(D1, E2)); 1.88 + h2 = safeadd(h3, safeadd(E1, A2)); 1.89 + h3 = safeadd(h4, safeadd(A1, B2)); 1.90 + h4 = safeadd(h0, safeadd(B1, C2)); 1.91 + h0 = T; 1.92 + } 1.93 + return [h0, h1, h2, h3, h4]; 1.94 +} 1.95 + 1.96 +function rmd160f(j, x, y, z) 1.97 +{ 1.98 + return ( 0 <= j && j <= 15) ? (x ^ y ^ z) : 1.99 + (16 <= j && j <= 31) ? (x & y) | (~x & z) : 1.100 + (32 <= j && j <= 47) ? (x | ~y) ^ z : 1.101 + (48 <= j && j <= 63) ? (x & z) | (y & ~z) : 1.102 + (64 <= j && j <= 79) ? x ^ (y | ~z) : 1.103 + "rmd160f: j out of range"; 1.104 +} 1.105 + 1.106 +function rmd160K1(j) 1.107 +{ 1.108 + return ( 0 <= j && j <= 15) ? 0x00000000 : 1.109 + (16 <= j && j <= 31) ? 0x5a827999 : 1.110 + (32 <= j && j <= 47) ? 0x6ed9eba1 : 1.111 + (48 <= j && j <= 63) ? 0x8f1bbcdc : 1.112 + (64 <= j && j <= 79) ? 0xa953fd4e : 1.113 + "rmd160K1: j out of range"; 1.114 +} 1.115 + 1.116 +function rmd160K2(j) 1.117 +{ 1.118 + return ( 0 <= j && j <= 15) ? 0x50a28be6 : 1.119 + (16 <= j && j <= 31) ? 0x5c4dd124 : 1.120 + (32 <= j && j <= 47) ? 0x6d703ef3 : 1.121 + (48 <= j && j <= 63) ? 0x7a6d76e9 : 1.122 + (64 <= j && j <= 79) ? 0x00000000 : 1.123 + "rmd160K2: j out of range"; 1.124 +} 1.125 + 1.126 +var rmd160r1 = [ 1.127 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1.128 + 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 1.129 + 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, 1.130 + 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 1.131 + 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 1.132 +]; 1.133 +var rmd160r2 = [ 1.134 + 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 1.135 + 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 1.136 + 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 1.137 + 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, 1.138 + 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 1.139 +]; 1.140 +var rmd160s1 = [ 1.141 + 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 1.142 + 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, 1.143 + 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 1.144 + 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, 1.145 + 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 1.146 +]; 1.147 +var rmd160s2 = [ 1.148 + 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, 1.149 + 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, 1.150 + 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 1.151 + 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, 1.152 + 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 1.153 +]; 1.154 + 1.155 +/* 1.156 + * Calculate the HMAC-RMD160 of a key and some data 1.157 + */ 1.158 +function corehmacrmd160(key, data) 1.159 +{ 1.160 + var bkey = str2binl(key); 1.161 + if(bkey.length > 16) bkey = corermd160(bkey, key.length * chrsz); 1.162 + 1.163 + var ipad = Array(16), opad = Array(16); 1.164 + for(var i = 0; i < 16; i++) 1.165 + { 1.166 + ipad[i] = bkey[i] ^ 0x36363636; 1.167 + opad[i] = bkey[i] ^ 0x5C5C5C5C; 1.168 + } 1.169 + 1.170 + var hash = corermd160(ipad.concat(str2binl(data)), 512 + data.length * chrsz); 1.171 + return corermd160(opad.concat(hash), 512 + 160); 1.172 +} 1.173 + 1.174 +/* 1.175 + * Add integers, wrapping at 2^32. This uses 16-bit operations internally 1.176 + * to work around bugs in some JS interpreters. 1.177 + */ 1.178 +function safeadd(x, y) 1.179 +{ 1.180 + var lsw = (x & 0xFFFF) + (y & 0xFFFF); 1.181 + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 1.182 + return (msw << 16) | (lsw & 0xFFFF); 1.183 +} 1.184 + 1.185 +/* 1.186 + * Bitwise rotate a 32-bit number to the left. 1.187 + */ 1.188 +function rol(num, cnt) 1.189 +{ 1.190 + return (num << cnt) | (num >>> (32 - cnt)); 1.191 +} 1.192 + 1.193 +/* 1.194 + * Convert a string to an array of little-endian words 1.195 + * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. 1.196 + */ 1.197 +function str2binl(str) 1.198 +{ 1.199 + var bin = Array(); 1.200 + var mask = (1 << chrsz) - 1; 1.201 + for(var i = 0; i < str.length * chrsz; i += chrsz) 1.202 + bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); 1.203 + return bin; 1.204 +} 1.205 + 1.206 +/* 1.207 + * Convert an array of little-endian words to a string 1.208 + */ 1.209 +function binl2str(bin) 1.210 +{ 1.211 + var str = ""; 1.212 + var mask = (1 << chrsz) - 1; 1.213 + for(var i = 0; i < bin.length * 32; i += chrsz) 1.214 + str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); 1.215 + return str; 1.216 +} 1.217 + 1.218 +/* 1.219 + * Convert an array of little-endian words to a hex string. 1.220 + */ 1.221 +function binl2hex(binarray) 1.222 +{ 1.223 + var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 1.224 + var str = ""; 1.225 + for(var i = 0; i < binarray.length * 4; i++) 1.226 + { 1.227 + str += hextab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 1.228 + hextab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); 1.229 + } 1.230 + return str; 1.231 +} 1.232 + 1.233 +/* 1.234 + * Convert an array of little-endian words to a base-64 string 1.235 + */ 1.236 +function binl2b64(binarray) 1.237 +{ 1.238 + var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 1.239 + var str = ""; 1.240 + for(var i = 0; i < binarray.length * 4; i += 3) 1.241 + { 1.242 + var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) 1.243 + | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) 1.244 + | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); 1.245 + for(var j = 0; j < 4; j++) 1.246 + { 1.247 + if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; 1.248 + else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); 1.249 + } 1.250 + } 1.251 + return str; 1.252 +}