1 2var Qqchose = {}; // Qqchose namespace 3 4/** 5 * 6 * @param {String} msg String to be hashed 7 * @param {Boolean} [utf8encode=true] Encode msg as UTF-8 before generating hash 8 * @returns {String} Hash of msg as hex character string 9 */ 10Qqchose.bout = function(msg, utf8encode) { 11 utf8encode = (typeof utf8encode == 'undefined') ? true : utf8encode; 12 13 if (utf8encode) msg = Utf8.encode(msg); 14 15 // constants [§4.2.1] 16 var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6]; 17 18 // PREPROCESSING 19 20 msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1] 21 22 // convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1] 23 var l = msg.length/4 + 2; // length (in 32-bit integers) of msg + 1 + appended length 24 var N = Math.ceil(l/16); // number of 16-integer-blocks required to hold 'l' ints 25 var M = new Array(N); 26 27 for (var i=0; i<N; i++) { 28 M[i] = new Array(16); 29 for (var j=0; j<16; j++) { // encode 4 chars per integer, big-endian encoding 30 M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) | 31 (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3)); 32 } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0 33 } 34 // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1] 35 // note: most significant word would be (len-1)*8 >>> 32, but since JS converts 36 // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators 37 M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14]) 38 M[N-1][15] = ((msg.length-1)*8) & 0xffffffff; 39 40 // set initial hash value [§5.3.1] 41 var H0 = 0x67452301; 42 var H1 = 0xefcdab89; 43 var H2 = 0x98badcfe; 44 var H3 = 0x10325476; 45 var H4 = 0xc3d2e1f0; 46 47 // HASH COMPUTATION [§6.1.2] 48 49 var W = new Array(80); var a, b, c, d, e; 50 for (var i=0; i<N; i++) { 51 52 // 1 - prepare message schedule 'W' 53 for (var t=0; t<16; t++) W[t] = M[i][t]; 54 for (var t=16; t<80; t++) W[t] = Qqchose.ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); 55 56 // 2 - initialise five working variables a, b, c, d, e with previous hash value 57 a = H0; b = H1; c = H2; d = H3; e = H4; 58 59 // 3 - main loop 60 for (var t=0; t<80; t++) { 61 var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants 62 var T = (Qqchose.ROTL(a,5) + Qqchose.f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff; 63 e = d; 64 d = c; 65 c = Qqchose.ROTL(b, 30); 66 b = a; 67 a = T; 68 } 69 70 // 4 - compute the new intermediate hash value 71 H0 = (H0+a) & 0xffffffff; // note 'addition modulo 2^32' 72 H1 = (H1+b) & 0xffffffff; 73 H2 = (H2+c) & 0xffffffff; 74 H3 = (H3+d) & 0xffffffff; 75 H4 = (H4+e) & 0xffffffff; 76 } 77 78 return Qqchose.toHexStr(H0) + Qqchose.toHexStr(H1) + 79 Qqchose.toHexStr(H2) + Qqchose.toHexStr(H3) + Qqchose.toHexStr(H4); 80} 81 82// 83// function 'f' [§4.1.1] 84// 85Qqchose.f = function(s, x, y, z) { 86 switch (s) { 87 case 0: return (x & y) ^ (~x & z); // Ch() 88 case 1: return x ^ y ^ z; // Parity() 89 case 2: return (x & y) ^ (x & z) ^ (y & z); // Maj() 90 case 3: return x ^ y ^ z; // Parity() 91 } 92} 93 94// 95// rotate left (circular left shift) value x by n positions [§3.2.5] 96// 97Qqchose.ROTL = function(x, n) { 98 return (x<<n) | (x>>>(32-n)); 99} 100 101// 102// hexadecimal representation of a number 103// (note toString(16) is implementation-dependant, and 104// in IE returns signed numbers when used on full words) 105// 106Qqchose.toHexStr = function(n) { 107 var s="", v; 108 for (var i=7; i>=0; i--) { v = (n>>>(i*4)) & 0xf; s += v.toString(16); } 109 return s; 110} 111 112 113/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 114/* Utf8 class: encode / decode between multi-byte Unicode characters and UTF-8 multiple */ 115/* single-byte character encoding (c) Chris Veness 2002-2010 */ 116/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 117 118var Utf8 = {}; // Utf8 namespace 119 120/** 121 * Encode multi-byte Unicode string into utf-8 multiple single-byte characters 122 * (BMP / basic multilingual plane only) 123 * 124 * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars 125 * 126 * @param {String} strUni Unicode string to be encoded as UTF-8 127 * @returns {String} encoded string 128 */ 129Utf8.encode = function(strUni) { 130 // use regular expressions & String.replace callback function for better efficiency 131 // than procedural approaches 132 var strUtf = strUni.replace( 133 /[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz 134 function(c) {
135 var cc = c.charCodeAt(0); 136 return String.fromCharCode(0xc0 | cc>>6, 0x80 | cc&0x3f); } 137 ); 138 strUtf = strUtf.replace( 139 /[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz 140 function(c) { 141 var cc = c.charCodeAt(0); 142 return String.fromCharCode(0xe0 | cc>>12, 0x80 | cc>>6&0x3F, 0x80 | cc&0x3f); } 143 ); 144 return strUtf; 145} 146 147/** 148 * Decode utf-8 encoded string back into multi-byte Unicode characters 149 * 150 * @param {String} strUtf UTF-8 string to be decoded back to Unicode 151 * @returns {String} decoded string 152 */ 153Utf8.decode = function(strUtf) { 154 // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char! 155 var strUni = strUtf.replace( 156 /[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars 157 function(c) { // (note parentheses for precence) 158 var cc = ((c.charCodeAt(0)&0x0f)<<12) | ((c.charCodeAt(1)&0x3f)<<6) | ( c.charCodeAt(2)&0x3f); 159 return String.fromCharCode(cc); } 160 ); 161 strUni = strUni.replace( 162 /[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars 163 function(c) { // (note parentheses for precence) 164 var cc = (c.charCodeAt(0)&0x1f)<<6 | c.charCodeAt(1)&0x3f; 165 return String.fromCharCode(cc); } 166 ); 167 return strUni; 168}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.