Skip to content

Instantly share code, notes, and snippets.

@ryanjduffy
Last active December 12, 2015 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanjduffy/4686906 to your computer and use it in GitHub Desktop.
Save ryanjduffy/4686906 to your computer and use it in GitHub Desktop.
/*
* jssha256 version 0.1 - Copyright 2006 B. Poettering
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
/*
* http://point-at-infinity.org/jssha256/
*
* This is a JavaScript implementation of the SHA256 secure hash function
* and the HMAC-SHA256 message authentication code (MAC).
*
* The routines' well-functioning has been verified with the test vectors
* given in FIPS-180-2, Appendix B and IETF RFC 4231. The HMAC algorithm
* conforms to IETF RFC 2104.
*
* The following code example computes the hash value of the string "abc".
*
* SHA256_init();
* SHA256_write("abc");
* digest = SHA256_finalize();
* digest_hex = array_to_hex_string(digest);
*
* Get the same result by calling the shortcut function SHA256_hash:
*
* digest_hex = SHA256_hash("abc");
*
* In the following example the calculation of the HMAC of the string "abc"
* using the key "secret key" is shown:
*
* HMAC_SHA256_init("secret key");
* HMAC_SHA256_write("abc");
* mac = HMAC_SHA256_finalize();
* mac_hex = array_to_hex_string(mac);
*
* Again, the same can be done more conveniently:
*
* mac_hex = HMAC_SHA256_MAC("secret key", "abc");
*
* Note that the internal state of the hash function is held in global
* variables. Therefore one hash value calculation has to be completed
* before the next is begun. The same applies the the HMAC routines.
*
* Report bugs to: jssha256 AT point-at-infinity.org
*
*/
/******************************************************************************/
/* Two all purpose helper functions follow */
/* string_to_array: convert a string to a character (byte) array */
function string_to_array(str) {
var len = str.length;
var res = new Array(len);
for (var i = 0; i < len; i++)
res[i] = str.charCodeAt(i);
return res;
}
/* array_to_hex_string: convert a byte array to a hexadecimal string */
function array_to_hex_string(ary) {
var res = "";
for (var i = 0; i < ary.length; i++)
res += SHA256_hexchars[ary[i] >> 4] + SHA256_hexchars[ary[i] & 0x0f];
return res;
}
/******************************************************************************/
/* The following are the SHA256 routines */
/*
SHA256_init: initialize the internal state of the hash function. Call this
function before calling the SHA256_write function.
*/
function SHA256_init() {
SHA256_H = new Array(0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19);
SHA256_buf = new Array();
SHA256_len = 0;
}
/*
SHA256_write: add a message fragment to the hash function's internal state.
'msg' may be given as string or as byte array and may have arbitrary length.
*/
function SHA256_write(msg) {
if (typeof (msg) == "string") SHA256_buf = SHA256_buf.concat(string_to_array(msg));
else SHA256_buf = SHA256_buf.concat(msg);
for (var i = 0; i + 64 <= SHA256_buf.length; i += 64)
SHA256_Hash_Byte_Block(SHA256_H, SHA256_buf.slice(i, i + 64));
SHA256_buf = SHA256_buf.slice(i);
SHA256_len += msg.length;
}
/*
SHA256_finalize: finalize the hash value calculation. Call this function
after the last call to SHA256_write. An array of 32 bytes (= 256 bits)
is returned.
*/
function SHA256_finalize() {
SHA256_buf[SHA256_buf.length] = 0x80;
if (SHA256_buf.length > 64 - 8) {
for (var i = SHA256_buf.length; i < 64; i++)
SHA256_buf[i] = 0;
SHA256_Hash_Byte_Block(SHA256_H, SHA256_buf);
SHA256_buf.length = 0;
}
for (var i = SHA256_buf.length; i < 64 - 5; i++)
SHA256_buf[i] = 0;
SHA256_buf[59] = (SHA256_len >>> 29) & 0xff;
SHA256_buf[60] = (SHA256_len >>> 21) & 0xff;
SHA256_buf[61] = (SHA256_len >>> 13) & 0xff;
SHA256_buf[62] = (SHA256_len >>> 5) & 0xff;
SHA256_buf[63] = (SHA256_len << 3) & 0xff;
SHA256_Hash_Byte_Block(SHA256_H, SHA256_buf);
var res = new Array(32);
for (var i = 0; i < 8; i++) {
res[4 * i + 0] = SHA256_H[i] >>> 24;
res[4 * i + 1] = (SHA256_H[i] >> 16) & 0xff;
res[4 * i + 2] = (SHA256_H[i] >> 8) & 0xff;
res[4 * i + 3] = SHA256_H[i] & 0xff;
}
delete SHA256_H;
delete SHA256_buf;
delete SHA256_len;
return res;
}
/*
SHA256_hash: calculate the hash value of the string or byte array 'msg'
and return it as hexadecimal string. This shortcut function may be more
convenient than calling SHA256_init, SHA256_write, SHA256_finalize
and array_to_hex_string explicitly.
*/
function SHA256_hash(msg) {
var res;
SHA256_init();
SHA256_write(msg);
res = SHA256_finalize();
return array_to_hex_string(res);
}
/******************************************************************************/
/* The following are the HMAC-SHA256 routines */
/*
HMAC_SHA256_init: initialize the MAC's internal state. The MAC key 'key'
may be given as string or as byte array and may have arbitrary length.
*/
function HMAC_SHA256_init(key) {
if (typeof (key) == "string") HMAC_SHA256_key = string_to_array(key);
else HMAC_SHA256_key = new Array().concat(key);
if (HMAC_SHA256_key.length > 64) {
SHA256_init();
SHA256_write(HMAC_SHA256_key);
HMAC_SHA256_key = SHA256_finalize();
}
for (var i = HMAC_SHA256_key.length; i < 64; i++)
HMAC_SHA256_key[i] = 0;
for (var i = 0; i < 64; i++)
HMAC_SHA256_key[i] ^= 0x36;
SHA256_init();
SHA256_write(HMAC_SHA256_key);
}
/*
HMAC_SHA256_write: process a message fragment. 'msg' may be given as
string or as byte array and may have arbitrary length.
*/
function HMAC_SHA256_write(msg) {
SHA256_write(msg);
}
/*
HMAC_SHA256_finalize: finalize the HMAC calculation. An array of 32 bytes
(= 256 bits) is returned.
*/
function HMAC_SHA256_finalize() {
var md = SHA256_finalize();
for (var i = 0; i < 64; i++)
HMAC_SHA256_key[i] ^= 0x36 ^ 0x5c;
SHA256_init();
SHA256_write(HMAC_SHA256_key);
SHA256_write(md);
for (var i = 0; i < 64; i++)
HMAC_SHA256_key[i] = 0;
delete HMAC_SHA256_key;
return SHA256_finalize();
}
/*
HMAC_SHA256_MAC: calculate the HMAC value of message 'msg' under key 'key'
(both may be of type string or byte array); return the MAC as hexadecimal
string. This shortcut function may be more convenient than calling
HMAC_SHA256_init, HMAC_SHA256_write, HMAC_SHA256_finalize and
array_to_hex_string explicitly.
*/
function HMAC_SHA256_MAC(key, msg) {
var res;
HMAC_SHA256_init(key);
HMAC_SHA256_write(msg);
res = HMAC_SHA256_finalize();
return array_to_hex_string(res);
}
/******************************************************************************/
/* The following lookup tables and functions are for internal use only! */
SHA256_hexchars = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f');
SHA256_K = new Array(
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
function SHA256_sigma0(x) {
return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
}
function SHA256_sigma1(x) {
return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10);
}
function SHA256_Sigma0(x) {
return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^ ((x >>> 22) | (x << 10));
}
function SHA256_Sigma1(x) {
return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^ ((x >>> 25) | (x << 7));
}
function SHA256_Ch(x, y, z) {
return z ^ (x & (y ^ z));
}
function SHA256_Maj(x, y, z) {
return (x & y) ^ (z & (x ^ y));
}
function SHA256_Hash_Word_Block(H, W) {
for (var i = 16; i < 64; i++)
W[i] = (SHA256_sigma1(W[i - 2]) + W[i - 7] + SHA256_sigma0(W[i - 15]) + W[i - 16]) & 0xffffffff;
var state = new Array().concat(H);
for (var i = 0; i < 64; i++) {
var T1 = state[7] + SHA256_Sigma1(state[4]) + SHA256_Ch(state[4], state[5], state[6]) + SHA256_K[i] + W[i];
var T2 = SHA256_Sigma0(state[0]) + SHA256_Maj(state[0], state[1], state[2]);
state.pop();
state.unshift((T1 + T2) & 0xffffffff);
state[4] = (state[4] + T1) & 0xffffffff;
}
for (var i = 0; i < 8; i++)
H[i] = (H[i] + state[i]) & 0xffffffff;
}
function SHA256_Hash_Byte_Block(H, w) {
var W = new Array(16);
for (var i = 0; i < 16; i++)
W[i] = w[4 * i + 0] << 24 | w[4 * i + 1] << 16 | w[4 * i + 2] << 8 | w[4 * i + 3];
SHA256_Hash_Word_Block(H, W);
}
// from http://www.myersdaily.org/joseph/javascript/md5.js
function md5cycle(x, k) {
var a = x[0],
b = x[1],
c = x[2],
d = x[3];
a = ff(a, b, c, d, k[0], 7, -680876936);
d = ff(d, a, b, c, k[1], 12, -389564586);
c = ff(c, d, a, b, k[2], 17, 606105819);
b = ff(b, c, d, a, k[3], 22, -1044525330);
a = ff(a, b, c, d, k[4], 7, -176418897);
d = ff(d, a, b, c, k[5], 12, 1200080426);
c = ff(c, d, a, b, k[6], 17, -1473231341);
b = ff(b, c, d, a, k[7], 22, -45705983);
a = ff(a, b, c, d, k[8], 7, 1770035416);
d = ff(d, a, b, c, k[9], 12, -1958414417);
c = ff(c, d, a, b, k[10], 17, -42063);
b = ff(b, c, d, a, k[11], 22, -1990404162);
a = ff(a, b, c, d, k[12], 7, 1804603682);
d = ff(d, a, b, c, k[13], 12, -40341101);
c = ff(c, d, a, b, k[14], 17, -1502002290);
b = ff(b, c, d, a, k[15], 22, 1236535329);
a = gg(a, b, c, d, k[1], 5, -165796510);
d = gg(d, a, b, c, k[6], 9, -1069501632);
c = gg(c, d, a, b, k[11], 14, 643717713);
b = gg(b, c, d, a, k[0], 20, -373897302);
a = gg(a, b, c, d, k[5], 5, -701558691);
d = gg(d, a, b, c, k[10], 9, 38016083);
c = gg(c, d, a, b, k[15], 14, -660478335);
b = gg(b, c, d, a, k[4], 20, -405537848);
a = gg(a, b, c, d, k[9], 5, 568446438);
d = gg(d, a, b, c, k[14], 9, -1019803690);
c = gg(c, d, a, b, k[3], 14, -187363961);
b = gg(b, c, d, a, k[8], 20, 1163531501);
a = gg(a, b, c, d, k[13], 5, -1444681467);
d = gg(d, a, b, c, k[2], 9, -51403784);
c = gg(c, d, a, b, k[7], 14, 1735328473);
b = gg(b, c, d, a, k[12], 20, -1926607734);
a = hh(a, b, c, d, k[5], 4, -378558);
d = hh(d, a, b, c, k[8], 11, -2022574463);
c = hh(c, d, a, b, k[11], 16, 1839030562);
b = hh(b, c, d, a, k[14], 23, -35309556);
a = hh(a, b, c, d, k[1], 4, -1530992060);
d = hh(d, a, b, c, k[4], 11, 1272893353);
c = hh(c, d, a, b, k[7], 16, -155497632);
b = hh(b, c, d, a, k[10], 23, -1094730640);
a = hh(a, b, c, d, k[13], 4, 681279174);
d = hh(d, a, b, c, k[0], 11, -358537222);
c = hh(c, d, a, b, k[3], 16, -722521979);
b = hh(b, c, d, a, k[6], 23, 76029189);
a = hh(a, b, c, d, k[9], 4, -640364487);
d = hh(d, a, b, c, k[12], 11, -421815835);
c = hh(c, d, a, b, k[15], 16, 530742520);
b = hh(b, c, d, a, k[2], 23, -995338651);
a = ii(a, b, c, d, k[0], 6, -198630844);
d = ii(d, a, b, c, k[7], 10, 1126891415);
c = ii(c, d, a, b, k[14], 15, -1416354905);
b = ii(b, c, d, a, k[5], 21, -57434055);
a = ii(a, b, c, d, k[12], 6, 1700485571);
d = ii(d, a, b, c, k[3], 10, -1894986606);
c = ii(c, d, a, b, k[10], 15, -1051523);
b = ii(b, c, d, a, k[1], 21, -2054922799);
a = ii(a, b, c, d, k[8], 6, 1873313359);
d = ii(d, a, b, c, k[15], 10, -30611744);
c = ii(c, d, a, b, k[6], 15, -1560198380);
b = ii(b, c, d, a, k[13], 21, 1309151649);
a = ii(a, b, c, d, k[4], 6, -145523070);
d = ii(d, a, b, c, k[11], 10, -1120210379);
c = ii(c, d, a, b, k[2], 15, 718787259);
b = ii(b, c, d, a, k[9], 21, -343485551);
x[0] = add32(a, x[0]);
x[1] = add32(b, x[1]);
x[2] = add32(c, x[2]);
x[3] = add32(d, x[3]);
}
function cmn(q, a, b, x, s, t) {
a = add32(add32(a, q), add32(x, t));
return add32((a << s) | (a >>> (32 - s)), b);
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function md51(s) {
txt = '';
var n = s.length,
state = [1732584193, -271733879, -1732584194, 271733878],
i;
for (i = 64; i <= s.length; i += 64) {
md5cycle(state, md5blk(s.substring(i - 64, i)));
}
s = s.substring(i - 64);
var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (i = 0; i < s.length; i++)
tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
tail[i >> 2] |= 0x80 << ((i % 4) << 3);
if (i > 55) {
md5cycle(state, tail);
for (i = 0; i < 16; i++) tail[i] = 0;
}
tail[14] = n * 8;
md5cycle(state, tail);
return state;
}
/* there needs to be support for Unicode here,
* unless we pretend that we can redefine the MD-5
* algorithm for multi-byte characters (perhaps
* by adding every four 16-bit characters and
* shortening the sum to 32 bits). Otherwise
* I suggest performing MD-5 as if every character
* was two bytes--e.g., 0040 0025 = @%--but then
* how will an ordinary MD-5 sum be matched?
* There is no way to standardize text to something
* like UTF-8 before transformation; speed cost is
* utterly prohibitive. The JavaScript standard
* itself needs to look at this: it should start
* providing access to strings as preformed UTF-8
* 8-bit unsigned value arrays.
*/
function md5blk(s) { /* I figured global was faster. */
var md5blks = [],
i; /* Andy King said do it this way. */
for (i = 0; i < 64; i += 4) {
md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
}
return md5blks;
}
var hex_chr = '0123456789abcdef'.split('');
function rhex(n) {
var s = '',
j = 0;
for (; j < 4; j++)
s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + hex_chr[(n >> (j * 8)) & 0x0F];
return s;
}
function hex(x) {
for (var i = 0; i < x.length; i++)
x[i] = rhex(x[i]);
return x.join('');
}
function md5(s) {
return hex(md51(s));
}
/* this function is much faster,
so if possible we use it. Some IEs
are the only ones I know of that
need the idiotic second function,
generated by an if clause. */
function add32(a, b) {
return (a + b) & 0xFFFFFFFF;
}
if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') {
function add32(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF),
msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
}
var Pusher = function (appId, key, secret) {
this.appId = appId;
this.key = key;
this.secret = secret;
}
Pusher.prototype = {
version: "1.0",
host:"http://api.pusherapp.com",
triggerEvent: function (name, channels, data, callback) {
var body = JSON.stringify({
name: name,
channels: (channels instanceof Array) ? channels : [channels],
data: JSON.stringify(data)
});
console.log("sending "+name);
var auth_key = "auth_key=" + this.key;
var auth_timestamp = "auth_timestamp=" + new Date().getTime()/1000;
var auth_version = "auth_version=" + this.version;
var body_md5 = "body_md5=" + md5(body);
var path = ["", "apps", this.appId, "events"].join("/");
var qs = [auth_key, auth_timestamp, auth_version, body_md5];
var signatureString = ["POST", path, qs.join("&")].join("\n");
var signature = HMAC_SHA256_MAC(this.secret, signatureString);
qs.push("auth_signature=" + signature);
Parse.Cloud.httpRequest({
method: 'POST',
url: this.host+path+"?"+qs.join("&"),
headers: {
'Content-Type': 'application/json'
},
body: body,
success: callback.success,
error: callback.error
});
}
}
exports.Pusher = Pusher;
function string_to_array(e){var t=e.length;var n=new Array(t);for(var r=0;r<t;r++)n[r]=e.charCodeAt(r);return n}function array_to_hex_string(e){var t="";for(var n=0;n<e.length;n++)t+=SHA256_hexchars[e[n]>>4]+SHA256_hexchars[e[n]&15];return t}function SHA256_init(){SHA256_H=new Array(1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225);SHA256_buf=new Array;SHA256_len=0}function SHA256_write(e){if(typeof e=="string")SHA256_buf=SHA256_buf.concat(string_to_array(e));else SHA256_buf=SHA256_buf.concat(e);for(var t=0;t+64<=SHA256_buf.length;t+=64)SHA256_Hash_Byte_Block(SHA256_H,SHA256_buf.slice(t,t+64));SHA256_buf=SHA256_buf.slice(t);SHA256_len+=e.length}function SHA256_finalize(){SHA256_buf[SHA256_buf.length]=128;if(SHA256_buf.length>64-8){for(var e=SHA256_buf.length;e<64;e++)SHA256_buf[e]=0;SHA256_Hash_Byte_Block(SHA256_H,SHA256_buf);SHA256_buf.length=0}for(var e=SHA256_buf.length;e<64-5;e++)SHA256_buf[e]=0;SHA256_buf[59]=SHA256_len>>>29&255;SHA256_buf[60]=SHA256_len>>>21&255;SHA256_buf[61]=SHA256_len>>>13&255;SHA256_buf[62]=SHA256_len>>>5&255;SHA256_buf[63]=SHA256_len<<3&255;SHA256_Hash_Byte_Block(SHA256_H,SHA256_buf);var t=new Array(32);for(var e=0;e<8;e++){t[4*e+0]=SHA256_H[e]>>>24;t[4*e+1]=SHA256_H[e]>>16&255;t[4*e+2]=SHA256_H[e]>>8&255;t[4*e+3]=SHA256_H[e]&255}delete SHA256_H;delete SHA256_buf;delete SHA256_len;return t}function SHA256_hash(e){var t;SHA256_init();SHA256_write(e);t=SHA256_finalize();return array_to_hex_string(t)}function HMAC_SHA256_init(e){if(typeof e=="string")HMAC_SHA256_key=string_to_array(e);else HMAC_SHA256_key=(new Array).concat(e);if(HMAC_SHA256_key.length>64){SHA256_init();SHA256_write(HMAC_SHA256_key);HMAC_SHA256_key=SHA256_finalize()}for(var t=HMAC_SHA256_key.length;t<64;t++)HMAC_SHA256_key[t]=0;for(var t=0;t<64;t++)HMAC_SHA256_key[t]^=54;SHA256_init();SHA256_write(HMAC_SHA256_key)}function HMAC_SHA256_write(e){SHA256_write(e)}function HMAC_SHA256_finalize(){var e=SHA256_finalize();for(var t=0;t<64;t++)HMAC_SHA256_key[t]^=54^92;SHA256_init();SHA256_write(HMAC_SHA256_key);SHA256_write(e);for(var t=0;t<64;t++)HMAC_SHA256_key[t]=0;delete HMAC_SHA256_key;return SHA256_finalize()}function HMAC_SHA256_MAC(e,t){var n;HMAC_SHA256_init(e);HMAC_SHA256_write(t);n=HMAC_SHA256_finalize();return array_to_hex_string(n)}function SHA256_sigma0(e){return(e>>>7|e<<25)^(e>>>18|e<<14)^e>>>3}function SHA256_sigma1(e){return(e>>>17|e<<15)^(e>>>19|e<<13)^e>>>10}function SHA256_Sigma0(e){return(e>>>2|e<<30)^(e>>>13|e<<19)^(e>>>22|e<<10)}function SHA256_Sigma1(e){return(e>>>6|e<<26)^(e>>>11|e<<21)^(e>>>25|e<<7)}function SHA256_Ch(e,t,n){return n^e&(t^n)}function SHA256_Maj(e,t,n){return e&t^n&(e^t)}function SHA256_Hash_Word_Block(e,t){for(var n=16;n<64;n++)t[n]=SHA256_sigma1(t[n-2])+t[n-7]+SHA256_sigma0(t[n-15])+t[n-16]&4294967295;var r=(new Array).concat(e);for(var n=0;n<64;n++){var i=r[7]+SHA256_Sigma1(r[4])+SHA256_Ch(r[4],r[5],r[6])+SHA256_K[n]+t[n];var s=SHA256_Sigma0(r[0])+SHA256_Maj(r[0],r[1],r[2]);r.pop();r.unshift(i+s&4294967295);r[4]=r[4]+i&4294967295}for(var n=0;n<8;n++)e[n]=e[n]+r[n]&4294967295}function SHA256_Hash_Byte_Block(e,t){var n=new Array(16);for(var r=0;r<16;r++)n[r]=t[4*r+0]<<24|t[4*r+1]<<16|t[4*r+2]<<8|t[4*r+3];SHA256_Hash_Word_Block(e,n)}function md5cycle(e,t){var n=e[0],r=e[1],i=e[2],s=e[3];n=ff(n,r,i,s,t[0],7,-680876936);s=ff(s,n,r,i,t[1],12,-389564586);i=ff(i,s,n,r,t[2],17,606105819);r=ff(r,i,s,n,t[3],22,-1044525330);n=ff(n,r,i,s,t[4],7,-176418897);s=ff(s,n,r,i,t[5],12,1200080426);i=ff(i,s,n,r,t[6],17,-1473231341);r=ff(r,i,s,n,t[7],22,-45705983);n=ff(n,r,i,s,t[8],7,1770035416);s=ff(s,n,r,i,t[9],12,-1958414417);i=ff(i,s,n,r,t[10],17,-42063);r=ff(r,i,s,n,t[11],22,-1990404162);n=ff(n,r,i,s,t[12],7,1804603682);s=ff(s,n,r,i,t[13],12,-40341101);i=ff(i,s,n,r,t[14],17,-1502002290);r=ff(r,i,s,n,t[15],22,1236535329);n=gg(n,r,i,s,t[1],5,-165796510);s=gg(s,n,r,i,t[6],9,-1069501632);i=gg(i,s,n,r,t[11],14,643717713);r=gg(r,i,s,n,t[0],20,-373897302);n=gg(n,r,i,s,t[5],5,-701558691);s=gg(s,n,r,i,t[10],9,38016083);i=gg(i,s,n,r,t[15],14,-660478335);r=gg(r,i,s,n,t[4],20,-405537848);n=gg(n,r,i,s,t[9],5,568446438);s=gg(s,n,r,i,t[14],9,-1019803690);i=gg(i,s,n,r,t[3],14,-187363961);r=gg(r,i,s,n,t[8],20,1163531501);n=gg(n,r,i,s,t[13],5,-1444681467);s=gg(s,n,r,i,t[2],9,-51403784);i=gg(i,s,n,r,t[7],14,1735328473);r=gg(r,i,s,n,t[12],20,-1926607734);n=hh(n,r,i,s,t[5],4,-378558);s=hh(s,n,r,i,t[8],11,-2022574463);i=hh(i,s,n,r,t[11],16,1839030562);r=hh(r,i,s,n,t[14],23,-35309556);n=hh(n,r,i,s,t[1],4,-1530992060);s=hh(s,n,r,i,t[4],11,1272893353);i=hh(i,s,n,r,t[7],16,-155497632);r=hh(r,i,s,n,t[10],23,-1094730640);n=hh(n,r,i,s,t[13],4,681279174);s=hh(s,n,r,i,t[0],11,-358537222);i=hh(i,s,n,r,t[3],16,-722521979);r=hh(r,i,s,n,t[6],23,76029189);n=hh(n,r,i,s,t[9],4,-640364487);s=hh(s,n,r,i,t[12],11,-421815835);i=hh(i,s,n,r,t[15],16,530742520);r=hh(r,i,s,n,t[2],23,-995338651);n=ii(n,r,i,s,t[0],6,-198630844);s=ii(s,n,r,i,t[7],10,1126891415);i=ii(i,s,n,r,t[14],15,-1416354905);r=ii(r,i,s,n,t[5],21,-57434055);n=ii(n,r,i,s,t[12],6,1700485571);s=ii(s,n,r,i,t[3],10,-1894986606);i=ii(i,s,n,r,t[10],15,-1051523);r=ii(r,i,s,n,t[1],21,-2054922799);n=ii(n,r,i,s,t[8],6,1873313359);s=ii(s,n,r,i,t[15],10,-30611744);i=ii(i,s,n,r,t[6],15,-1560198380);r=ii(r,i,s,n,t[13],21,1309151649);n=ii(n,r,i,s,t[4],6,-145523070);s=ii(s,n,r,i,t[11],10,-1120210379);i=ii(i,s,n,r,t[2],15,718787259);r=ii(r,i,s,n,t[9],21,-343485551);e[0]=add32(n,e[0]);e[1]=add32(r,e[1]);e[2]=add32(i,e[2]);e[3]=add32(s,e[3])}function cmn(e,t,n,r,i,s){t=add32(add32(t,e),add32(r,s));return add32(t<<i|t>>>32-i,n)}function ff(e,t,n,r,i,s,o){return cmn(t&n|~t&r,e,t,i,s,o)}function gg(e,t,n,r,i,s,o){return cmn(t&r|n&~r,e,t,i,s,o)}function hh(e,t,n,r,i,s,o){return cmn(t^n^r,e,t,i,s,o)}function ii(e,t,n,r,i,s,o){return cmn(n^(t|~r),e,t,i,s,o)}function md51(e){txt="";var t=e.length,n=[1732584193,-271733879,-1732584194,271733878],r;for(r=64;r<=e.length;r+=64){md5cycle(n,md5blk(e.substring(r-64,r)))}e=e.substring(r-64);var i=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(r=0;r<e.length;r++)i[r>>2]|=e.charCodeAt(r)<<(r%4<<3);i[r>>2]|=128<<(r%4<<3);if(r>55){md5cycle(n,i);for(r=0;r<16;r++)i[r]=0}i[14]=t*8;md5cycle(n,i);return n}function md5blk(e){var t=[],n;for(n=0;n<64;n+=4){t[n>>2]=e.charCodeAt(n)+(e.charCodeAt(n+1)<<8)+(e.charCodeAt(n+2)<<16)+(e.charCodeAt(n+3)<<24)}return t}function rhex(e){var t="",n=0;for(;n<4;n++)t+=hex_chr[e>>n*8+4&15]+hex_chr[e>>n*8&15];return t}function hex(e){for(var t=0;t<e.length;t++)e[t]=rhex(e[t]);return e.join("")}function md5(e){return hex(md51(e))}function add32(e,t){return e+t&4294967295}SHA256_hexchars=new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");SHA256_K=new Array(1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298);var hex_chr="0123456789abcdef".split("");if(md5("hello")!="5d41402abc4b2a76b9719d911017c592"){function add32(e,t){var n=(e&65535)+(t&65535),r=(e>>16)+(t>>16)+(n>>16);return r<<16|n&65535}}var Pusher=function(e,t,n){this.appId=e;this.key=t;this.secret=n};Pusher.prototype={version:"1.0",host:"http://api.pusherapp.com",triggerEvent:function(e,t,n,r){var i=JSON.stringify({name:e,channels:t instanceof Array?t:[t],data:JSON.stringify(n)});console.log("sending "+e);var s="auth_key="+this.key;var o="auth_timestamp="+(new Date).getTime()/1e3;var u="auth_version="+this.version;var a="body_md5="+md5(i);var f=["","apps",this.appId,"events"].join("/");var l=[s,o,u,a];var c=["POST",f,l.join("&")].join("\n");var h=HMAC_SHA256_MAC(this.secret,c);l.push("auth_signature="+h);Parse.Cloud.httpRequest({method:"POST",url:this.host+f+"?"+l.join("&"),headers:{"Content-Type":"application/json"},body:i,success:r.success,error:r.error})}};exports.Pusher=Pusher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment