1 (function(e){if("function"==typeof bootstrap)bootstrap("ohauth",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeOhauth=e}else"undefined"!=typeof window?window.ohauth=e():global.ohauth=e()})(function(){var define,ses,bootstrap,module,exports;
2 return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
5 var hashes = require('jshashes'),
6 xtend = require('xtend'),
7 sha1 = new hashes.SHA1();
11 ohauth.qsString = function(obj) {
12 return Object.keys(obj).sort().map(function(key) {
13 return ohauth.percentEncode(key) + '=' +
14 ohauth.percentEncode(obj[key]);
18 ohauth.stringQs = function(str) {
19 return str.split('&').filter(function (pair) {
21 }).reduce(function(obj, pair){
22 var parts = pair.split('=');
23 obj[decodeURIComponent(parts[0])] = (null === parts[1]) ?
24 '' : decodeURIComponent(parts[1]);
29 ohauth.rawxhr = function(method, url, data, headers, callback) {
30 var xhr = new XMLHttpRequest(),
31 twoHundred = /^20\d$/;
32 xhr.onreadystatechange = function() {
33 if (4 == xhr.readyState && 0 !== xhr.status) {
34 if (twoHundred.test(xhr.status)) callback(null, xhr);
35 else return callback(xhr, null);
38 xhr.onerror = function(e) { return callback(e, null); };
39 xhr.open(method, url, true);
40 for (var h in headers) xhr.setRequestHeader(h, headers[h]);
44 ohauth.xhr = function(method, url, auth, data, options, callback) {
45 var headers = (options && options.header) || {
46 'Content-Type': 'application/x-www-form-urlencoded'
48 headers.Authorization = 'OAuth ' + ohauth.authHeader(auth);
49 ohauth.rawxhr(method, url, data, headers, callback);
52 ohauth.nonce = function() {
53 for (var o = ''; o.length < 6;) {
54 o += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'[Math.floor(Math.random() * 61)];
59 ohauth.authHeader = function(obj) {
60 return Object.keys(obj).sort().map(function(key) {
61 return encodeURIComponent(key) + '="' + encodeURIComponent(obj[key]) + '"';
65 ohauth.timestamp = function() { return ~~((+new Date()) / 1000); };
67 ohauth.percentEncode = function(s) {
68 return encodeURIComponent(s)
69 .replace(/\!/g, '%21').replace(/\'/g, '%27')
70 .replace(/\*/g, '%2A').replace(/\(/g, '%28').replace(/\)/g, '%29');
73 ohauth.baseString = function(method, url, params) {
74 if (params.oauth_signature) delete params.oauth_signature;
77 ohauth.percentEncode(url),
78 ohauth.percentEncode(ohauth.qsString(params))].join('&');
81 ohauth.signature = function(oauth_secret, token_secret, baseString) {
83 ohauth.percentEncode(oauth_secret) + '&' +
84 ohauth.percentEncode(token_secret),
89 * Takes an options object for configuration (consumer_key,
90 * consumer_secret, version, signature_method, token) and returns a
91 * function that generates the Authorization header for given data.
93 * The returned function takes these parameters:
94 * - method: GET/POST/...
95 * - uri: full URI with protocol, port, path and query string
96 * - extra_params: any extra parameters (that are passed in the POST data),
97 * can be an object or a from-urlencoded string.
99 * Returned function returns full OAuth header with "OAuth" string in it.
102 ohauth.headerGenerator = function(options) {
103 options = options || {};
104 var consumer_key = options.consumer_key || '',
105 consumer_secret = options.consumer_secret || '',
106 signature_method = options.signature_method || 'HMAC-SHA1',
107 version = options.version || '1.0',
108 token = options.token || '',
109 token_secret = options.token_secret || '';
111 return function(method, uri, extra_params) {
112 method = method.toUpperCase();
113 if (typeof extra_params === 'string' && extra_params.length > 0) {
114 extra_params = ohauth.stringQs(extra_params);
117 var uri_parts = uri.split('?', 2),
118 base_uri = uri_parts[0];
120 var query_params = uri_parts.length === 2 ?
121 ohauth.stringQs(uri_parts[1]) : {};
124 oauth_consumer_key: consumer_key,
125 oauth_signature_method: signature_method,
126 oauth_version: version,
127 oauth_timestamp: ohauth.timestamp(),
128 oauth_nonce: ohauth.nonce()
131 if (token) oauth_params.oauth_token = token;
133 var all_params = xtend({}, oauth_params, query_params, extra_params),
134 base_str = ohauth.baseString(method, base_uri, all_params);
136 oauth_params.oauth_signature = ohauth.signature(consumer_secret, token_secret, base_str);
138 return 'OAuth ' + ohauth.authHeader(oauth_params);
142 module.exports = ohauth;
144 },{"xtend":2,"jshashes":3}],2:[function(require,module,exports){
145 var Keys = Object.keys || objectKeys
147 module.exports = extend
152 for (var i = 0; i < arguments.length; i++) {
153 var source = arguments[i]
155 if (!isObject(source)) {
159 var keys = Keys(source)
161 for (var j = 0; j < keys.length; j++) {
163 target[name] = source[name]
170 function objectKeys(obj) {
178 function isObject(obj) {
179 return obj !== null && typeof obj === "object"
182 },{}],3:[function(require,module,exports){
183 (function(global){/**
184 * jsHashes - A fast and independent hashing library pure JavaScript implemented (ES5 compliant) for both server and client side
187 * @author Tomas Aparicio <tomas@rijndael-project.com>
188 * @license New BSD (see LICENSE file)
191 * Algorithms specification:
193 * MD5 <http://www.ietf.org/rfc/rfc1321.txt>
194 * RIPEMD-160 <http://homes.esat.kuleuven.be/~bosselae/ripemd160.html>
195 * SHA1 <http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf>
196 * SHA256 <http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf>
197 * SHA512 <http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf>
198 * HMAC <http://www.ietf.org/rfc/rfc2104.txt>
204 // private helper methods
205 function utf8Encode(input) {
206 var x, y, output = '', i = -1, l = input.length;
208 /* Decode utf-16 surrogate pairs */
209 x = input.charCodeAt(i);
210 y = i + 1 < l ? input.charCodeAt(i + 1) : 0;
211 if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
212 x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
215 /* Encode output as utf-8 */
217 output += String.fromCharCode(x);
218 } else if (x <= 0x7FF) {
219 output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
221 } else if (x <= 0xFFFF) {
222 output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
223 0x80 | ((x >>> 6 ) & 0x3F),
225 } else if (x <= 0x1FFFFF) {
226 output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
227 0x80 | ((x >>> 12) & 0x3F),
228 0x80 | ((x >>> 6 ) & 0x3F),
235 function utf8Decode(str_data) {
236 var i, ac, c1, c2, c3, arr = [], l = str_data.length;
237 i = ac = c1 = c2 = c3 = 0;
241 c1 = str_data.charCodeAt(i);
244 arr[ac] = String.fromCharCode(c1);
246 } else if (c1 > 191 && c1 < 224) {
247 c2 = str_data.charCodeAt(i + 1);
248 arr[ac] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
251 c2 = str_data.charCodeAt(i + 1);
252 c3 = str_data.charCodeAt(i + 2);
253 arr[ac] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
261 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
262 * to work around bugs in some JS interpreters.
264 function safe_add(x, y) {
265 var lsw = (x & 0xFFFF) + (y & 0xFFFF),
266 msw = (x >> 16) + (y >> 16) + (lsw >> 16);
267 return (msw << 16) | (lsw & 0xFFFF);
271 * Bitwise rotate a 32-bit number to the left.
273 function bit_rol(num, cnt) {
274 return (num << cnt) | (num >>> (32 - cnt));
278 * Convert a raw string to a hex string
280 function rstr2hex(input, hexcase) {
281 var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef',
282 output = '', x, i = 0, l = input.length;
283 for (; i < l; i+=1) {
284 x = input.charCodeAt(i);
285 output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
291 * Encode a string as utf-16
293 function str2rstr_utf16le(input) {
294 var i, l = input.length, output = '';
295 for (i = 0; i < l; i+=1) {
296 output += String.fromCharCode( input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
301 function str2rstr_utf16be(input) {
302 var i, l = input.length, output = '';
303 for (i = 0; i < l; i+=1) {
304 output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
310 * Convert an array of big-endian words to a string
312 function binb2rstr(input) {
313 var i, l = input.length * 32, output = '';
314 for (i = 0; i < l; i += 8) {
315 output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
321 * Convert an array of little-endian words to a string
323 function binl2rstr(input) {
324 var i, l = input.length * 32, output = '';
325 for (i = 0;i < l; i += 8) {
326 output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
332 * Convert a raw string to an array of little-endian words
333 * Characters >255 have their high-byte silently ignored.
335 function rstr2binl(input) {
336 var i, l = input.length * 8, output = Array(input.length >> 2), lo = output.length;
337 for (i = 0; i < lo; i+=1) {
340 for (i = 0; i < l; i += 8) {
341 output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
347 * Convert a raw string to an array of big-endian words
348 * Characters >255 have their high-byte silently ignored.
350 function rstr2binb(input) {
351 var i, l = input.length * 8, output = Array(input.length >> 2), lo = output.length;
352 for (i = 0; i < lo; i+=1) {
355 for (i = 0; i < l; i += 8) {
356 output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
362 * Convert a raw string to an arbitrary string encoding
364 function rstr2any(input, encoding) {
365 var divisor = encoding.length,
366 remainders = Array(),
367 i, q, x, ld, quotient, dividend, output, full_length;
369 /* Convert to an array of 16-bit big-endian values, forming the dividend */
370 dividend = Array(Math.ceil(input.length / 2));
371 ld = dividend.length;
372 for (i = 0; i < ld; i+=1) {
373 dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
377 * Repeatedly perform a long division. The binary array forms the dividend,
378 * the length of the encoding is the divisor. Once computed, the quotient
379 * forms the dividend for the next step. We stop when the dividend is zerHashes.
380 * All remainders are stored for later use.
382 while(dividend.length > 0) {
385 for (i = 0; i < dividend.length; i+=1) {
386 x = (x << 16) + dividend[i];
387 q = Math.floor(x / divisor);
389 if (quotient.length > 0 || q > 0) {
390 quotient[quotient.length] = q;
393 remainders[remainders.length] = x;
397 /* Convert the remainders to the output string */
399 for (i = remainders.length - 1; i >= 0; i--) {
400 output += encoding.charAt(remainders[i]);
403 /* Append leading zero equivalents */
404 full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
405 for (i = output.length; i < full_length; i+=1) {
406 output = encoding[0] + output;
412 * Convert a raw string to a base-64 string
414 function rstr2b64(input, b64pad) {
415 var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
417 len = input.length, i, j, triplet;
418 b64pad= b64pad || '=';
419 for (i = 0; i < len; i += 3) {
420 triplet = (input.charCodeAt(i) << 16)
421 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
422 | (i + 2 < len ? input.charCodeAt(i+2) : 0);
423 for (j = 0; j < 4; j+=1) {
424 if (i * 8 + j * 6 > input.length * 8) {
427 output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
436 * @property {String} version
445 Base64 : function () {
446 // private properties
447 var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
448 pad = '=', // default pad according with the RFC standard
449 url = false, // URL encoding support @todo
450 utf8 = true; // by default enable UTF-8 support encoding
452 // public method for encoding
453 this.encode = function (input) {
459 input = (utf8) ? utf8Encode(input) : input;
461 for (i = 0; i < len; i += 3) {
462 triplet = (input.charCodeAt(i) << 16)
463 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
464 | (i + 2 < len ? input.charCodeAt(i+2) : 0);
465 for (j = 0; j < 4; j+=1) {
466 if (i * 8 + j * 6 > len * 8) {
469 output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
476 // public method for decoding
477 this.decode = function (input) {
478 // var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
479 var i, o1, o2, o3, h1, h2, h3, h4, bits, ac,
482 if (!input) { return input; }
485 input = input.replace(new RegExp('\\'+pad,'gi'),''); // use '='
488 do { // unpack four hexets into three octets using index points in b64
489 h1 = tab.indexOf(input.charAt(i+=1));
490 h2 = tab.indexOf(input.charAt(i+=1));
491 h3 = tab.indexOf(input.charAt(i+=1));
492 h4 = tab.indexOf(input.charAt(i+=1));
494 bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
496 o1 = bits >> 16 & 0xff;
497 o2 = bits >> 8 & 0xff;
502 arr[ac] = String.fromCharCode(o1);
503 } else if (h4 === 64) {
504 arr[ac] = String.fromCharCode(o1, o2);
506 arr[ac] = String.fromCharCode(o1, o2, o3);
508 } while (i < input.length);
511 dec = (utf8) ? utf8Decode(dec) : dec;
516 // set custom pad string
517 this.setPad = function (str) {
521 // set custom tab string characters
522 this.setTab = function (str) {
526 this.setUTF8 = function (bool) {
527 if (typeof bool === 'boolean') {
539 * @param {String} str Input String
542 CRC32 : function (str) {
543 var crc = 0, x = 0, y = 0, table, i, iTop;
544 str = utf8Encode(str);
547 '00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 ',
548 '79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 ',
549 '84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F ',
550 '63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD ',
551 'A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC ',
552 '51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 ',
553 'B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 ',
554 '06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 ',
555 'E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 ',
556 '12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 ',
557 'D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 ',
558 '33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 ',
559 'CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 ',
560 '9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E ',
561 '7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D ',
562 '806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 ',
563 '60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA ',
564 'AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 ',
565 '5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 ',
566 'B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 ',
567 '05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 ',
568 'F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA ',
569 '11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 ',
570 'D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F ',
571 '30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E ',
572 'C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'
576 for (i = 0, iTop = str.length; i < iTop; i+=1 ) {
577 y = ( crc ^ str.charCodeAt( i ) ) & 0xFF;
578 x = '0x' + table.substr( y * 9, 8 );
579 crc = ( crc >>> 8 ) ^ x;
581 // always return a positive number (that's what >>> 0 does)
582 return (crc ^ (-1)) >>> 0;
588 * @param {Object} [config]
590 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
591 * Digest Algorithm, as defined in RFC 1321.
592 * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
593 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
594 * See <http://pajhome.org.uk/crypt/md5> for more infHashes.
596 MD5 : function (options) {
598 * Private config properties. You may need to tweak these to be compatible with
599 * the server-side, but the defaults work in most cases.
600 * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
602 var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
603 b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
604 utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
606 // privileged (public) methods
607 this.hex = function (s) {
608 return rstr2hex(rstr(s, utf8), hexcase);
610 this.b64 = function (s) {
611 return rstr2b64(rstr(s), b64pad);
613 this.any = function(s, e) {
614 return rstr2any(rstr(s, utf8), e);
616 this.hex_hmac = function (k, d) {
617 return rstr2hex(rstr_hmac(k, d), hexcase);
619 this.b64_hmac = function (k, d) {
620 return rstr2b64(rstr_hmac(k,d), b64pad);
622 this.any_hmac = function (k, d, e) {
623 return rstr2any(rstr_hmac(k, d), e);
626 * Perform a simple self-test to see if the VM is working
627 * @return {String} Hexadecimal hash sample
629 this.vm_test = function () {
630 return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
633 * Enable/disable uppercase hexadecimal returned string
635 * @return {Object} this
637 this.setUpperCase = function (a) {
638 if (typeof a === 'boolean' ) {
644 * Defines a base64 pad string
645 * @param {String} Pad
646 * @return {Object} this
648 this.setPad = function (a) {
649 b64pad = a || b64pad;
653 * Defines a base64 pad string
655 * @return {Object} [this]
657 this.setUTF8 = function (a) {
658 if (typeof a === 'boolean') {
667 * Calculate the MD5 of a raw string
670 s = (utf8) ? utf8Encode(s): s;
671 return binl2rstr(binl(rstr2binl(s), s.length * 8));
675 * Calculate the HMAC-MD5, of a key and some data (raw strings)
677 function rstr_hmac(key, data) {
678 var bkey, ipad, opad, hash, i;
680 key = (utf8) ? utf8Encode(key) : key;
681 data = (utf8) ? utf8Encode(data) : data;
682 bkey = rstr2binl(key);
683 if (bkey.length > 16) {
684 bkey = binl(bkey, key.length * 8);
687 ipad = Array(16), opad = Array(16);
688 for (i = 0; i < 16; i+=1) {
689 ipad[i] = bkey[i] ^ 0x36363636;
690 opad[i] = bkey[i] ^ 0x5C5C5C5C;
692 hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
693 return binl2rstr(binl(opad.concat(hash), 512 + 128));
697 * Calculate the MD5 of an array of little-endian words, and a bit length.
699 function binl(x, len) {
700 var i, olda, oldb, oldc, oldd,
707 x[len >> 5] |= 0x80 << ((len) % 32);
708 x[(((len + 64) >>> 9) << 4) + 14] = len;
710 for (i = 0; i < x.length; i += 16) {
716 a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
717 d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
718 c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
719 b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
720 a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
721 d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
722 c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
723 b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
724 a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
725 d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
726 c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
727 b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
728 a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
729 d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
730 c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
731 b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
733 a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
734 d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
735 c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
736 b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
737 a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
738 d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
739 c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
740 b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
741 a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
742 d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
743 c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
744 b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
745 a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
746 d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
747 c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
748 b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
750 a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
751 d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
752 c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
753 b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
754 a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
755 d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
756 c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
757 b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
758 a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
759 d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
760 c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
761 b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
762 a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
763 d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
764 c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
765 b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
767 a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
768 d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
769 c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
770 b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
771 a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
772 d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
773 c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
774 b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
775 a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
776 d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
777 c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
778 b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
779 a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
780 d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
781 c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
782 b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
784 a = safe_add(a, olda);
785 b = safe_add(b, oldb);
786 c = safe_add(c, oldc);
787 d = safe_add(d, oldd);
789 return Array(a, b, c, d);
793 * These functions implement the four basic operations the algorithm uses.
795 function md5_cmn(q, a, b, x, s, t) {
796 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
798 function md5_ff(a, b, c, d, x, s, t) {
799 return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
801 function md5_gg(a, b, c, d, x, s, t) {
802 return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
804 function md5_hh(a, b, c, d, x, s, t) {
805 return md5_cmn(b ^ c ^ d, a, b, x, s, t);
807 function md5_ii(a, b, c, d, x, s, t) {
808 return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
814 * @param {Object} [config]
817 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS 180-1
818 * Version 2.2 Copyright Paul Johnston 2000 - 2009.
819 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
820 * See http://pajhome.org.uk/crypt/md5 for details.
822 SHA1 : function (options) {
824 * Private config properties. You may need to tweak these to be compatible with
825 * the server-side, but the defaults work in most cases.
826 * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
828 var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
829 b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
830 utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
833 this.hex = function (s) {
834 return rstr2hex(rstr(s, utf8), hexcase);
836 this.b64 = function (s) {
837 return rstr2b64(rstr(s, utf8), b64pad);
839 this.any = function (s, e) {
840 return rstr2any(rstr(s, utf8), e);
842 this.hex_hmac = function (k, d) {
843 return rstr2hex(rstr_hmac(k, d));
845 this.b64_hmac = function (k, d) {
846 return rstr2b64(rstr_hmac(k, d), b64pad);
848 this.any_hmac = function (k, d, e) {
849 return rstr2any(rstr_hmac(k, d), e);
852 * Perform a simple self-test to see if the VM is working
853 * @return {String} Hexadecimal hash sample
856 this.vm_test = function () {
857 return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
860 * @description Enable/disable uppercase hexadecimal returned string
862 * @return {Object} this
865 this.setUpperCase = function (a) {
866 if (typeof a === 'boolean') {
872 * @description Defines a base64 pad string
873 * @param {string} Pad
874 * @return {Object} this
877 this.setPad = function (a) {
878 b64pad = a || b64pad;
882 * @description Defines a base64 pad string
884 * @return {Object} this
887 this.setUTF8 = function (a) {
888 if (typeof a === 'boolean') {
897 * Calculate the SHA-512 of a raw string
900 s = (utf8) ? utf8Encode(s) : s;
901 return binb2rstr(binb(rstr2binb(s), s.length * 8));
905 * Calculate the HMAC-SHA1 of a key and some data (raw strings)
907 function rstr_hmac(key, data) {
908 var bkey, ipad, opad, i, hash;
909 key = (utf8) ? utf8Encode(key) : key;
910 data = (utf8) ? utf8Encode(data) : data;
911 bkey = rstr2binb(key);
913 if (bkey.length > 16) {
914 bkey = binb(bkey, key.length * 8);
916 ipad = Array(16), opad = Array(16);
917 for (i = 0; i < 16; i+=1) {
918 ipad[i] = bkey[i] ^ 0x36363636;
919 opad[i] = bkey[i] ^ 0x5C5C5C5C;
921 hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
922 return binb2rstr(binb(opad.concat(hash), 512 + 160));
926 * Calculate the SHA-1 of an array of big-endian words, and a bit length
928 function binb(x, len) {
929 var i, j, t, olda, oldb, oldc, oldd, olde,
938 x[len >> 5] |= 0x80 << (24 - len % 32);
939 x[((len + 64 >> 9) << 4) + 15] = len;
941 for (i = 0; i < x.length; i += 16) {
948 for (j = 0; j < 80; j+=1) {
952 w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
954 t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
955 safe_add(safe_add(e, w[j]), sha1_kt(j)));
963 a = safe_add(a, olda);
964 b = safe_add(b, oldb);
965 c = safe_add(c, oldc);
966 d = safe_add(d, oldd);
967 e = safe_add(e, olde);
969 return Array(a, b, c, d, e);
973 * Perform the appropriate triplet combination function for the current
976 function sha1_ft(t, b, c, d) {
977 if (t < 20) { return (b & c) | ((~b) & d); }
978 if (t < 40) { return b ^ c ^ d; }
979 if (t < 60) { return (b & c) | (b & d) | (c & d); }
984 * Determine the appropriate additive constant for the current iteration
986 function sha1_kt(t) {
987 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
988 (t < 60) ? -1894007588 : -899497514;
992 * @class Hashes.SHA256
995 * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined in FIPS 180-2
996 * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
997 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
998 * See http://pajhome.org.uk/crypt/md5 for details.
999 * Also http://anmar.eu.org/projects/jssha2/
1001 SHA256 : function (options) {
1003 * Private properties configuration variables. You may need to tweak these to be compatible with
1004 * the server-side, but the defaults work in most cases.
1005 * @see this.setUpperCase() method
1006 * @see this.setPad() method
1008 var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase */
1009 b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', /* base-64 pad character. Default '=' for strict RFC compliance */
1010 utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, /* enable/disable utf8 encoding */
1013 /* privileged (public) methods */
1014 this.hex = function (s) {
1015 return rstr2hex(rstr(s, utf8));
1017 this.b64 = function (s) {
1018 return rstr2b64(rstr(s, utf8), b64pad);
1020 this.any = function (s, e) {
1021 return rstr2any(rstr(s, utf8), e);
1023 this.hex_hmac = function (k, d) {
1024 return rstr2hex(rstr_hmac(k, d));
1026 this.b64_hmac = function (k, d) {
1027 return rstr2b64(rstr_hmac(k, d), b64pad);
1029 this.any_hmac = function (k, d, e) {
1030 return rstr2any(rstr_hmac(k, d), e);
1033 * Perform a simple self-test to see if the VM is working
1034 * @return {String} Hexadecimal hash sample
1037 this.vm_test = function () {
1038 return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
1041 * Enable/disable uppercase hexadecimal returned string
1043 * @return {Object} this
1046 this.setUpperCase = function (a) {
1047 if (typeof a === 'boolean') {
1053 * @description Defines a base64 pad string
1054 * @param {string} Pad
1055 * @return {Object} this
1058 this.setPad = function (a) {
1059 b64pad = a || b64pad;
1063 * Defines a base64 pad string
1065 * @return {Object} this
1068 this.setUTF8 = function (a) {
1069 if (typeof a === 'boolean') {
1078 * Calculate the SHA-512 of a raw string
1080 function rstr(s, utf8) {
1081 s = (utf8) ? utf8Encode(s) : s;
1082 return binb2rstr(binb(rstr2binb(s), s.length * 8));
1086 * Calculate the HMAC-sha256 of a key and some data (raw strings)
1088 function rstr_hmac(key, data) {
1089 key = (utf8) ? utf8Encode(key) : key;
1090 data = (utf8) ? utf8Encode(data) : data;
1092 bkey = rstr2binb(key),
1096 if (bkey.length > 16) { bkey = binb(bkey, key.length * 8); }
1098 for (; i < 16; i+=1) {
1099 ipad[i] = bkey[i] ^ 0x36363636;
1100 opad[i] = bkey[i] ^ 0x5C5C5C5C;
1103 hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
1104 return binb2rstr(binb(opad.concat(hash), 512 + 256));
1108 * Main sha256 function, with its support functions
1110 function sha256_S (X, n) {return ( X >>> n ) | (X << (32 - n));}
1111 function sha256_R (X, n) {return ( X >>> n );}
1112 function sha256_Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
1113 function sha256_Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
1114 function sha256_Sigma0256(x) {return (sha256_S(x, 2) ^ sha256_S(x, 13) ^ sha256_S(x, 22));}
1115 function sha256_Sigma1256(x) {return (sha256_S(x, 6) ^ sha256_S(x, 11) ^ sha256_S(x, 25));}
1116 function sha256_Gamma0256(x) {return (sha256_S(x, 7) ^ sha256_S(x, 18) ^ sha256_R(x, 3));}
1117 function sha256_Gamma1256(x) {return (sha256_S(x, 17) ^ sha256_S(x, 19) ^ sha256_R(x, 10));}
1118 function sha256_Sigma0512(x) {return (sha256_S(x, 28) ^ sha256_S(x, 34) ^ sha256_S(x, 39));}
1119 function sha256_Sigma1512(x) {return (sha256_S(x, 14) ^ sha256_S(x, 18) ^ sha256_S(x, 41));}
1120 function sha256_Gamma0512(x) {return (sha256_S(x, 1) ^ sha256_S(x, 8) ^ sha256_R(x, 7));}
1121 function sha256_Gamma1512(x) {return (sha256_S(x, 19) ^ sha256_S(x, 61) ^ sha256_R(x, 6));}
1124 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993,
1125 -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987,
1126 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522,
1127 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986,
1128 -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585,
1129 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291,
1130 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885,
1131 -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344,
1132 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218,
1133 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872,
1134 -1866530822, -1538233109, -1090935817, -965641998
1137 function binb(m, l) {
1138 var HASH = [1779033703, -1150833019, 1013904242, -1521486534,
1139 1359893119, -1694144372, 528734635, 1541459225];
1140 var W = new Array(64);
1141 var a, b, c, d, e, f, g, h;
1144 /* append padding */
1145 m[l >> 5] |= 0x80 << (24 - l % 32);
1146 m[((l + 64 >> 9) << 4) + 15] = l;
1148 for (i = 0; i < m.length; i += 16)
1159 for (j = 0; j < 64; j+=1)
1164 W[j] = safe_add(safe_add(safe_add(sha256_Gamma1256(W[j - 2]), W[j - 7]),
1165 sha256_Gamma0256(W[j - 15])), W[j - 16]);
1168 T1 = safe_add(safe_add(safe_add(safe_add(h, sha256_Sigma1256(e)), sha256_Ch(e, f, g)),
1169 sha256_K[j]), W[j]);
1170 T2 = safe_add(sha256_Sigma0256(a), sha256_Maj(a, b, c));
1174 e = safe_add(d, T1);
1178 a = safe_add(T1, T2);
1181 HASH[0] = safe_add(a, HASH[0]);
1182 HASH[1] = safe_add(b, HASH[1]);
1183 HASH[2] = safe_add(c, HASH[2]);
1184 HASH[3] = safe_add(d, HASH[3]);
1185 HASH[4] = safe_add(e, HASH[4]);
1186 HASH[5] = safe_add(f, HASH[5]);
1187 HASH[6] = safe_add(g, HASH[6]);
1188 HASH[7] = safe_add(h, HASH[7]);
1196 * @class Hashes.SHA512
1199 * A JavaScript implementation of the Secure Hash Algorithm, SHA-512, as defined in FIPS 180-2
1200 * Version 2.2 Copyright Anonymous Contributor, Paul Johnston 2000 - 2009.
1201 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
1202 * See http://pajhome.org.uk/crypt/md5 for details.
1204 SHA512 : function (options) {
1206 * Private properties configuration variables. You may need to tweak these to be compatible with
1207 * the server-side, but the defaults work in most cases.
1208 * @see this.setUpperCase() method
1209 * @see this.setPad() method
1211 var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false , /* hexadecimal output case format. false - lowercase; true - uppercase */
1212 b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', /* base-64 pad character. Default '=' for strict RFC compliance */
1213 utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, /* enable/disable utf8 encoding */
1216 /* privileged (public) methods */
1217 this.hex = function (s) {
1218 return rstr2hex(rstr(s));
1220 this.b64 = function (s) {
1221 return rstr2b64(rstr(s), b64pad);
1223 this.any = function (s, e) {
1224 return rstr2any(rstr(s), e);
1226 this.hex_hmac = function (k, d) {
1227 return rstr2hex(rstr_hmac(k, d));
1229 this.b64_hmac = function (k, d) {
1230 return rstr2b64(rstr_hmac(k, d), b64pad);
1232 this.any_hmac = function (k, d, e) {
1233 return rstr2any(rstr_hmac(k, d), e);
1236 * Perform a simple self-test to see if the VM is working
1237 * @return {String} Hexadecimal hash sample
1240 this.vm_test = function () {
1241 return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
1244 * @description Enable/disable uppercase hexadecimal returned string
1246 * @return {Object} this
1249 this.setUpperCase = function (a) {
1250 if (typeof a === 'boolean') {
1256 * @description Defines a base64 pad string
1257 * @param {string} Pad
1258 * @return {Object} this
1261 this.setPad = function (a) {
1262 b64pad = a || b64pad;
1266 * @description Defines a base64 pad string
1268 * @return {Object} this
1271 this.setUTF8 = function (a) {
1272 if (typeof a === 'boolean') {
1278 /* private methods */
1281 * Calculate the SHA-512 of a raw string
1284 s = (utf8) ? utf8Encode(s) : s;
1285 return binb2rstr(binb(rstr2binb(s), s.length * 8));
1288 * Calculate the HMAC-SHA-512 of a key and some data (raw strings)
1290 function rstr_hmac(key, data) {
1291 key = (utf8) ? utf8Encode(key) : key;
1292 data = (utf8) ? utf8Encode(data) : data;
1295 bkey = rstr2binb(key),
1296 ipad = Array(32), opad = Array(32);
1298 if (bkey.length > 32) { bkey = binb(bkey, key.length * 8); }
1300 for (; i < 32; i+=1) {
1301 ipad[i] = bkey[i] ^ 0x36363636;
1302 opad[i] = bkey[i] ^ 0x5C5C5C5C;
1305 hash = binb(ipad.concat(rstr2binb(data)), 1024 + data.length * 8);
1306 return binb2rstr(binb(opad.concat(hash), 1024 + 512));
1310 * Calculate the SHA-512 of an array of big-endian dwords, and a bit length
1312 function binb(x, len) {
1315 hash = new Array(16),
1316 //Initial hash values
1318 new int64(0x6a09e667, -205731576),
1319 new int64(-1150833019, -2067093701),
1320 new int64(0x3c6ef372, -23791573),
1321 new int64(-1521486534, 0x5f1d36f1),
1322 new int64(0x510e527f, -1377402159),
1323 new int64(-1694144372, 0x2b3e6c1f),
1324 new int64(0x1f83d9ab, -79577749),
1325 new int64(0x5be0cd19, 0x137e2179)
1327 T1 = new int64(0, 0),
1328 T2 = new int64(0, 0),
1337 //Temporary variables not specified by the document
1338 s0 = new int64(0, 0),
1339 s1 = new int64(0, 0),
1340 Ch = new int64(0, 0),
1341 Maj = new int64(0, 0),
1342 r1 = new int64(0, 0),
1343 r2 = new int64(0, 0),
1344 r3 = new int64(0, 0);
1346 if (sha512_k === undefined) {
1349 new int64(0x428a2f98, -685199838), new int64(0x71374491, 0x23ef65cd),
1350 new int64(-1245643825, -330482897), new int64(-373957723, -2121671748),
1351 new int64(0x3956c25b, -213338824), new int64(0x59f111f1, -1241133031),
1352 new int64(-1841331548, -1357295717), new int64(-1424204075, -630357736),
1353 new int64(-670586216, -1560083902), new int64(0x12835b01, 0x45706fbe),
1354 new int64(0x243185be, 0x4ee4b28c), new int64(0x550c7dc3, -704662302),
1355 new int64(0x72be5d74, -226784913), new int64(-2132889090, 0x3b1696b1),
1356 new int64(-1680079193, 0x25c71235), new int64(-1046744716, -815192428),
1357 new int64(-459576895, -1628353838), new int64(-272742522, 0x384f25e3),
1358 new int64(0xfc19dc6, -1953704523), new int64(0x240ca1cc, 0x77ac9c65),
1359 new int64(0x2de92c6f, 0x592b0275), new int64(0x4a7484aa, 0x6ea6e483),
1360 new int64(0x5cb0a9dc, -1119749164), new int64(0x76f988da, -2096016459),
1361 new int64(-1740746414, -295247957), new int64(-1473132947, 0x2db43210),
1362 new int64(-1341970488, -1728372417), new int64(-1084653625, -1091629340),
1363 new int64(-958395405, 0x3da88fc2), new int64(-710438585, -1828018395),
1364 new int64(0x6ca6351, -536640913), new int64(0x14292967, 0xa0e6e70),
1365 new int64(0x27b70a85, 0x46d22ffc), new int64(0x2e1b2138, 0x5c26c926),
1366 new int64(0x4d2c6dfc, 0x5ac42aed), new int64(0x53380d13, -1651133473),
1367 new int64(0x650a7354, -1951439906), new int64(0x766a0abb, 0x3c77b2a8),
1368 new int64(-2117940946, 0x47edaee6), new int64(-1838011259, 0x1482353b),
1369 new int64(-1564481375, 0x4cf10364), new int64(-1474664885, -1136513023),
1370 new int64(-1035236496, -789014639), new int64(-949202525, 0x654be30),
1371 new int64(-778901479, -688958952), new int64(-694614492, 0x5565a910),
1372 new int64(-200395387, 0x5771202a), new int64(0x106aa070, 0x32bbd1b8),
1373 new int64(0x19a4c116, -1194143544), new int64(0x1e376c08, 0x5141ab53),
1374 new int64(0x2748774c, -544281703), new int64(0x34b0bcb5, -509917016),
1375 new int64(0x391c0cb3, -976659869), new int64(0x4ed8aa4a, -482243893),
1376 new int64(0x5b9cca4f, 0x7763e373), new int64(0x682e6ff3, -692930397),
1377 new int64(0x748f82ee, 0x5defb2fc), new int64(0x78a5636f, 0x43172f60),
1378 new int64(-2067236844, -1578062990), new int64(-1933114872, 0x1a6439ec),
1379 new int64(-1866530822, 0x23631e28), new int64(-1538233109, -561857047),
1380 new int64(-1090935817, -1295615723), new int64(-965641998, -479046869),
1381 new int64(-903397682, -366583396), new int64(-779700025, 0x21c0c207),
1382 new int64(-354779690, -840897762), new int64(-176337025, -294727304),
1383 new int64(0x6f067aa, 0x72176fba), new int64(0xa637dc5, -1563912026),
1384 new int64(0x113f9804, -1090974290), new int64(0x1b710b35, 0x131c471b),
1385 new int64(0x28db77f5, 0x23047d84), new int64(0x32caab7b, 0x40c72493),
1386 new int64(0x3c9ebe0a, 0x15c9bebc), new int64(0x431d67c4, -1676669620),
1387 new int64(0x4cc5d4be, -885112138), new int64(0x597f299c, -60457430),
1388 new int64(0x5fcb6fab, 0x3ad6faec), new int64(0x6c44198c, 0x4a475817)
1392 for (i=0; i<80; i+=1) {
1393 W[i] = new int64(0, 0);
1396 // append padding to the source string. The format is described in the FIPS.
1397 x[len >> 5] |= 0x80 << (24 - (len & 0x1f));
1398 x[((len + 128 >> 10)<< 5) + 31] = len;
1400 for (i = 0; i<l; i+=32) { //32 dwords is the block size
1410 for (j=0; j<16; j+=1) {
1411 W[j].h = x[i + 2*j];
1412 W[j].l = x[i + 2*j + 1];
1415 for (j=16; j<80; j+=1) {
1417 int64rrot(r1, W[j-2], 19);
1418 int64revrrot(r2, W[j-2], 29);
1419 int64shr(r3, W[j-2], 6);
1420 s1.l = r1.l ^ r2.l ^ r3.l;
1421 s1.h = r1.h ^ r2.h ^ r3.h;
1423 int64rrot(r1, W[j-15], 1);
1424 int64rrot(r2, W[j-15], 8);
1425 int64shr(r3, W[j-15], 7);
1426 s0.l = r1.l ^ r2.l ^ r3.l;
1427 s0.h = r1.h ^ r2.h ^ r3.h;
1429 int64add4(W[j], s1, W[j-7], s0, W[j-16]);
1432 for (j = 0; j < 80; j+=1) {
1434 Ch.l = (e.l & f.l) ^ (~e.l & g.l);
1435 Ch.h = (e.h & f.h) ^ (~e.h & g.h);
1438 int64rrot(r1, e, 14);
1439 int64rrot(r2, e, 18);
1440 int64revrrot(r3, e, 9);
1441 s1.l = r1.l ^ r2.l ^ r3.l;
1442 s1.h = r1.h ^ r2.h ^ r3.h;
1445 int64rrot(r1, a, 28);
1446 int64revrrot(r2, a, 2);
1447 int64revrrot(r3, a, 7);
1448 s0.l = r1.l ^ r2.l ^ r3.l;
1449 s0.h = r1.h ^ r2.h ^ r3.h;
1452 Maj.l = (a.l & b.l) ^ (a.l & c.l) ^ (b.l & c.l);
1453 Maj.h = (a.h & b.h) ^ (a.h & c.h) ^ (b.h & c.h);
1455 int64add5(T1, h, s1, Ch, sha512_k[j], W[j]);
1456 int64add(T2, s0, Maj);
1465 int64add(a, T1, T2);
1467 int64add(H[0], H[0], a);
1468 int64add(H[1], H[1], b);
1469 int64add(H[2], H[2], c);
1470 int64add(H[3], H[3], d);
1471 int64add(H[4], H[4], e);
1472 int64add(H[5], H[5], f);
1473 int64add(H[6], H[6], g);
1474 int64add(H[7], H[7], h);
1477 //represent the hash as an array of 32-bit dwords
1478 for (i=0; i<8; i+=1) {
1480 hash[2*i + 1] = H[i].l;
1485 //A constructor for 64-bit numbers
1486 function int64(h, l) {
1489 //this.toString = int64toString;
1492 //Copies src into dst, assuming both are 64-bit numbers
1493 function int64copy(dst, src) {
1498 //Right-rotates a 64-bit number by shift
1499 //Won't handle cases of shift>=32
1500 //The function revrrot() is for that
1501 function int64rrot(dst, x, shift) {
1502 dst.l = (x.l >>> shift) | (x.h << (32-shift));
1503 dst.h = (x.h >>> shift) | (x.l << (32-shift));
1506 //Reverses the dwords of the source and then rotates right by shift.
1507 //This is equivalent to rotation by 32+shift
1508 function int64revrrot(dst, x, shift) {
1509 dst.l = (x.h >>> shift) | (x.l << (32-shift));
1510 dst.h = (x.l >>> shift) | (x.h << (32-shift));
1513 //Bitwise-shifts right a 64-bit number by shift
1514 //Won't handle shift>=32, but it's never needed in SHA512
1515 function int64shr(dst, x, shift) {
1516 dst.l = (x.l >>> shift) | (x.h << (32-shift));
1517 dst.h = (x.h >>> shift);
1520 //Adds two 64-bit numbers
1521 //Like the original implementation, does not rely on 32-bit operations
1522 function int64add(dst, x, y) {
1523 var w0 = (x.l & 0xffff) + (y.l & 0xffff);
1524 var w1 = (x.l >>> 16) + (y.l >>> 16) + (w0 >>> 16);
1525 var w2 = (x.h & 0xffff) + (y.h & 0xffff) + (w1 >>> 16);
1526 var w3 = (x.h >>> 16) + (y.h >>> 16) + (w2 >>> 16);
1527 dst.l = (w0 & 0xffff) | (w1 << 16);
1528 dst.h = (w2 & 0xffff) | (w3 << 16);
1531 //Same, except with 4 addends. Works faster than adding them one by one.
1532 function int64add4(dst, a, b, c, d) {
1533 var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff);
1534 var w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (w0 >>> 16);
1535 var w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (w1 >>> 16);
1536 var w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (w2 >>> 16);
1537 dst.l = (w0 & 0xffff) | (w1 << 16);
1538 dst.h = (w2 & 0xffff) | (w3 << 16);
1541 //Same, except with 5 addends
1542 function int64add5(dst, a, b, c, d, e) {
1543 var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff) + (e.l & 0xffff),
1544 w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (e.l >>> 16) + (w0 >>> 16),
1545 w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (e.h & 0xffff) + (w1 >>> 16),
1546 w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (e.h >>> 16) + (w2 >>> 16);
1547 dst.l = (w0 & 0xffff) | (w1 << 16);
1548 dst.h = (w2 & 0xffff) | (w3 << 16);
1552 * @class Hashes.RMD160
1554 * @param {Object} [config]
1556 * A JavaScript implementation of the RIPEMD-160 Algorithm
1557 * Version 2.2 Copyright Jeremy Lin, Paul Johnston 2000 - 2009.
1558 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
1559 * See http://pajhome.org.uk/crypt/md5 for details.
1560 * Also http://www.ocf.berkeley.edu/~jjlin/jsotp/
1562 RMD160 : function (options) {
1564 * Private properties configuration variables. You may need to tweak these to be compatible with
1565 * the server-side, but the defaults work in most cases.
1566 * @see this.setUpperCase() method
1567 * @see this.setPad() method
1569 var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, /* hexadecimal output case format. false - lowercase; true - uppercase */
1570 b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', /* base-64 pad character. Default '=' for strict RFC compliance */
1571 utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, /* enable/disable utf8 encoding */
1573 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1574 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
1575 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1576 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
1577 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
1580 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
1581 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
1582 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
1583 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
1584 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
1587 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
1588 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
1589 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
1590 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
1591 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
1594 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
1595 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
1596 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
1597 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
1598 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
1601 /* privileged (public) methods */
1602 this.hex = function (s) {
1603 return rstr2hex(rstr(s, utf8));
1605 this.b64 = function (s) {
1606 return rstr2b64(rstr(s, utf8), b64pad);
1608 this.any = function (s, e) {
1609 return rstr2any(rstr(s, utf8), e);
1611 this.hex_hmac = function (k, d) {
1612 return rstr2hex(rstr_hmac(k, d));
1614 this.b64_hmac = function (k, d) {
1615 return rstr2b64(rstr_hmac(k, d), b64pad);
1617 this.any_hmac = function (k, d, e) {
1618 return rstr2any(rstr_hmac(k, d), e);
1621 * Perform a simple self-test to see if the VM is working
1622 * @return {String} Hexadecimal hash sample
1625 this.vm_test = function () {
1626 return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
1629 * @description Enable/disable uppercase hexadecimal returned string
1631 * @return {Object} this
1634 this.setUpperCase = function (a) {
1635 if (typeof a === 'boolean' ) { hexcase = a; }
1639 * @description Defines a base64 pad string
1640 * @param {string} Pad
1641 * @return {Object} this
1644 this.setPad = function (a) {
1645 if (typeof a !== 'undefined' ) { b64pad = a; }
1649 * @description Defines a base64 pad string
1651 * @return {Object} this
1654 this.setUTF8 = function (a) {
1655 if (typeof a === 'boolean') { utf8 = a; }
1659 /* private methods */
1662 * Calculate the rmd160 of a raw string
1665 s = (utf8) ? utf8Encode(s) : s;
1666 return binl2rstr(binl(rstr2binl(s), s.length * 8));
1670 * Calculate the HMAC-rmd160 of a key and some data (raw strings)
1672 function rstr_hmac(key, data) {
1673 key = (utf8) ? utf8Encode(key) : key;
1674 data = (utf8) ? utf8Encode(data) : data;
1676 bkey = rstr2binl(key),
1677 ipad = Array(16), opad = Array(16);
1679 if (bkey.length > 16) {
1680 bkey = binl(bkey, key.length * 8);
1683 for (i = 0; i < 16; i+=1) {
1684 ipad[i] = bkey[i] ^ 0x36363636;
1685 opad[i] = bkey[i] ^ 0x5C5C5C5C;
1687 hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
1688 return binl2rstr(binl(opad.concat(hash), 512 + 160));
1692 * Convert an array of little-endian words to a string
1694 function binl2rstr(input) {
1695 var i, output = '', l = input.length * 32;
1696 for (i = 0; i < l; i += 8) {
1697 output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
1703 * Calculate the RIPE-MD160 of an array of little-endian words, and a bit length.
1705 function binl(x, len) {
1715 /* append padding */
1716 x[len >> 5] |= 0x80 << (len % 32);
1717 x[(((len + 64) >>> 9) << 4) + 14] = len;
1720 for (i = 0; i < l; i+=16) {
1721 A1 = A2 = h0; B1 = B2 = h1; C1 = C2 = h2; D1 = D2 = h3; E1 = E2 = h4;
1722 for (j = 0; j <= 79; j+=1) {
1723 T = safe_add(A1, rmd160_f(j, B1, C1, D1));
1724 T = safe_add(T, x[i + rmd160_r1[j]]);
1725 T = safe_add(T, rmd160_K1(j));
1726 T = safe_add(bit_rol(T, rmd160_s1[j]), E1);
1727 A1 = E1; E1 = D1; D1 = bit_rol(C1, 10); C1 = B1; B1 = T;
1728 T = safe_add(A2, rmd160_f(79-j, B2, C2, D2));
1729 T = safe_add(T, x[i + rmd160_r2[j]]);
1730 T = safe_add(T, rmd160_K2(j));
1731 T = safe_add(bit_rol(T, rmd160_s2[j]), E2);
1732 A2 = E2; E2 = D2; D2 = bit_rol(C2, 10); C2 = B2; B2 = T;
1735 T = safe_add(h1, safe_add(C1, D2));
1736 h1 = safe_add(h2, safe_add(D1, E2));
1737 h2 = safe_add(h3, safe_add(E1, A2));
1738 h3 = safe_add(h4, safe_add(A1, B2));
1739 h4 = safe_add(h0, safe_add(B1, C2));
1742 return [h0, h1, h2, h3, h4];
1745 // specific algorithm methods
1746 function rmd160_f(j, x, y, z) {
1747 return ( 0 <= j && j <= 15) ? (x ^ y ^ z) :
1748 (16 <= j && j <= 31) ? (x & y) | (~x & z) :
1749 (32 <= j && j <= 47) ? (x | ~y) ^ z :
1750 (48 <= j && j <= 63) ? (x & z) | (y & ~z) :
1751 (64 <= j && j <= 79) ? x ^ (y | ~z) :
1752 'rmd160_f: j out of range';
1755 function rmd160_K1(j) {
1756 return ( 0 <= j && j <= 15) ? 0x00000000 :
1757 (16 <= j && j <= 31) ? 0x5a827999 :
1758 (32 <= j && j <= 47) ? 0x6ed9eba1 :
1759 (48 <= j && j <= 63) ? 0x8f1bbcdc :
1760 (64 <= j && j <= 79) ? 0xa953fd4e :
1761 'rmd160_K1: j out of range';
1764 function rmd160_K2(j){
1765 return ( 0 <= j && j <= 15) ? 0x50a28be6 :
1766 (16 <= j && j <= 31) ? 0x5c4dd124 :
1767 (32 <= j && j <= 47) ? 0x6d703ef3 :
1768 (48 <= j && j <= 63) ? 0x7a6d76e9 :
1769 (64 <= j && j <= 79) ? 0x00000000 :
1770 'rmd160_K2: j out of range';
1776 (function( window, undefined ) {
1777 var freeExports = false;
1778 if (typeof exports === 'object' ) {
1779 freeExports = exports;
1780 if (exports && typeof global === 'object' && global && global === global.global ) { window = global; }
1783 if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
1784 // define as an anonymous module, so, through path mapping, it can be aliased
1785 define(function () { return Hashes; });
1787 else if ( freeExports ) {
1788 // in Node.js or RingoJS v0.8.0+
1789 if ( typeof module === 'object' && module && module.exports === freeExports ) {
1790 module.exports = Hashes;
1792 // in Narwhal or RingoJS v0.7.0-
1794 freeExports.Hashes = Hashes;
1798 // in a browser or Rhino
1799 window.Hashes = Hashes;