Find maximum of 9 numbers - fast - javascript

I have 9 random integer numbers. Is there a faster way how to find maximum without explicitly compare everything?
I am performing this operation milion times per frame and this is quite slow.
function calculateMax(a, b, c,
d, e, f,
g, h, i){
var max = (a > b) ? a : b;
var max2 = (c > d) ? c : d;
var max3 = (e > f) ? e : f;
var max4 = (g > h) ? g : h;
max = (i > max) ? i : max;
max = (max2 > max) ? max2 : max;
max = (max3 > max) ? max3 : max;
max = (max4 > max) ? max4 : max;
return max;
}

The results seem rather disappointing after running few times, but posting in case someone wants to do better test on other browsers, or improve (might be faster with SIMD instructions)
function max1(a, b, c, d, e, f, g, h, i) { // shortened calculateMax
var max = (a > b) ? a : b; var max2 = (c > d) ? c : d;
var max3 = (e > f) ? e : f; var max4 = (g > h) ? g : h;
max = ( i > max) ? i : max; max = (max2 > max) ? max2 : max;
max = (max3 > max) ? max3 : max; max = (max4 > max) ? max4 : max;
return max;
}
function max2(a, b, c, d, e, f, g, h, i) {
if (a < b) a = b; if (a < c) a = c; if (a < d) a = d; if (a < e) a = e;
if (a < f) a = f; if (a < g) a = g; if (a < h) a = h; if (a < i) a = i;
return a;
}
//function max(a, b) { return a - ((a -= b) & (a >> 31 )); }
//function max(a, b) { return (a - b >>> 31) * b | (b - a >>> 31) * a }
function max(a, b) { return (a - b >> 31) & b | (b - a >> 31) & a }
function max3(a, b, c, d, e, f, g, h, i) {
return max(a, max(b, max(c, max(d, max(e, max(f, max(g, max(h, i))))))))
}
function max4(a, b, c, d, e, f, g, h, i) {
a = (a - b >> 31) & b | (b - a >> 31) & a
a = (a - c >> 31) & c | (c - a >> 31) & a
a = (a - d >> 31) & d | (d - a >> 31) & a
a = (a - e >> 31) & e | (e - a >> 31) & a
a = (a - f >> 31) & f | (f - a >> 31) & a
a = (a - g >> 31) & g | (g - a >> 31) & a
a = (a - h >> 31) & h | (h - a >> 31) & a
return (a - i >> 31) & i | (i - a >> 31) & a
}
var l = console.log, p = performance
var t = p.now(), m = max1(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 1, -t)
var t = p.now(), m = max2(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 2, -t)
var t = p.now(), m = max3(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 3, -t)
var t = p.now(), m = max4(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 4, -t)

The best way would be to use native implementation:
Array.max = function( array ){
return Math.max.apply( Math, array );
};
console.log(Array.max([1,2,9,8,3,2,1,2,5])); // 9
cf: https://johnresig.com/blog/fast-javascript-maxmin/

Related

Integrating Old Javascript Functions into React-Native

I'm currently using this package of Javascript on my website. And I need to be able to use it in the app that I'm trying to build (I'm VERY new to react-native--a PHP, javascript developer here).
How do I integrate it? Do I have to convert it or is it possible to just include it and call the functions? I have no idea. Sigh. I've searched but I didn't find anything that answered my question--though that could be because I'm new to RN and don't know what the heck I'm doing :(.
Thanks in advance for your advice!
To use the functions, I currently just call them with the second line in Javascript:
var strINeedHashed = "Hash Me Please";
var hash = hex_sha256(strINeedHashed);
Here's the JS I need access to:
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
function safe_add (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function S (X, n) {return ( X >>> n ) | (X << (32 - n));}
function R (X, n) {return ( X >>> n );}
function Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
function Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
function Sigma0256(x) {return (S(x, 2) ^ S(x, 13) ^ S(x, 22));}
function Sigma1256(x) {return (S(x, 6) ^ S(x, 11) ^ S(x, 25));}
function Gamma0256(x) {return (S(x, 7) ^ S(x, 18) ^ R(x, 3));}
function Gamma1256(x) {return (S(x, 17) ^ S(x, 19) ^ R(x, 10));}
function core_sha256 (m, l) {
var K = new Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,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);
var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
var W = new Array(64);
var a, b, c, d, e, f, g, h, i, j;
var T1, T2;
/* append padding */
m[l >> 5] |= 0x80 << (24 - l % 32);
m[((l + 64 >> 9) << 4) + 15] = l;
for ( var i = 0; i<m.length; i+=16 ) {
a = HASH[0]; b = HASH[1]; c = HASH[2]; d = HASH[3]; e = HASH[4]; f = HASH[5]; g = HASH[6]; h = HASH[7];
for ( var j = 0; j<64; j++) {
if (j < 16) W[j] = m[j + i];
else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
T2 = safe_add(Sigma0256(a), Maj(a, b, c));
h = g; g = f; f = e; e = safe_add(d, T1); d = c; c = b; b = a; a = safe_add(T1, T2);
}
HASH[0] = safe_add(a, HASH[0]); HASH[1] = safe_add(b, HASH[1]); HASH[2] = safe_add(c, HASH[2]); HASH[3] = safe_add(d, HASH[3]); HASH[4] = safe_add(e, HASH[4]); HASH[5] = safe_add(f, HASH[5]); HASH[6] = safe_add(g, HASH[6]); HASH[7] = safe_add(h, HASH[7]);
}
return HASH;
}
function str2binb (str) {
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32);
return bin;
}
function binb2hex (binarray) {
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for (var i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
}
return str;
}
function hex_sha256(s){return binb2hex(core_sha256(str2binb(s),s.length * chrsz));}
if that is working javascript, you just add it to your project's code.
Fastest, a bit ugly way, is to include that code in the file where you using it. More proper way would be to make of file for that and include es6 exports for functions that you need to access.
React Native uses JavaScript, so in theory if you function is written in JavaScript you should have no issues integrating it into your React Native project. Just simply add your old function to the React Native project and just call it as you normally would :)

Decompression Routine In Javascript to Python

I'm trying to rewrite a javascript file decompression routine to python. But I can't seem to get it right. The function always returns None on python.
Here is the original JS:
var c = new Uint8Array(b),
d = 0,
e = new Uint32Array(4096),
f = new Uint32Array(4096),
g = 256,
h = a.length,
k = 0,
l = 1,
m = 0,
n = 1;
c[d++] = a[0];
for (var r = 1;; r++) {
n = r + (r >> 1);
if (n + 1 >= h) break;
var m = a[n + 1],
n = a[n],
p = r & 1 ? m << 4 | n >> 4 : (m & 15) << 8 | n;
if (p < g)
if (256 > p) m = d, n = 1, c[d++] = p;
else
for (var m = d, n = f[p], p = e[p], q = p + n; p < q;) c[d++] = c[p++];
else if (p == g) {
m = d;
n = l + 1;
p = k;
for (q = k + l; p < q;) c[d++] = c[p++];
c[d++] = c[k]
} else break;
e[g] = k;
f[g++] = l + 1;
k = m;
l = n;
g = 4096 <= g ? 256 : g
}
return d == b ? c : null
And here is my python implementation, what did I do wrong? (Improved with #le_m answer but still returning None.)
c = [0] * b
d = 0
e = [0] * 4096
f = [0] * 4096
g = 256
h = len(a)
k = 0
l = 1
m = 0
n = 1
c[d] = a[0]
d += 1
r = 1
while True:
n = r + (r >> 1)
if (n + 1) >= h:
break
m = a[n + 1]
n = a[n]
p = (m << 4 | n >> 4) if r & 1 else ((m & 15) << 8 | n)
if (p < g):
if (256 > p):
m = d
n = 1
c[d] = p
d += 1
else:
m = d
n = f[p]
p = e[p]
q = p + n
while p < q:
c[d] = c[p]
d += 1
p += 1
elif p == g:
m = d
n = 1 + 1
p = k
q = k + l
while p < q:
c[d] = c[p]
d += 1
p += 1
else:
break
e[g] = k
f[g] = l + 1
g += 1
k = m
l = n
g = 256 if 4096 <= g else g
r += 1
Just from glancing at the code, I spot the following differences:
new Uint8Array(b) translates to [0] * b as the typed array is initialized with zeros. Same holds for the other typed arrays.
c[d++] = a[0] translates to c[d] = a[0] followed by d += 1, not the other way round. Same holds for the other post-increments, too.
The loop counter r += 1 should go to the end of the loop body or should be initialized with 0 instead.
I recommend stepping through both implementations step by step with a debugger (your browser's developer console for JavaScript) and compare the variable values.

Javascript using HMAC SHA256 with base64 encoded key

I am using the following function to create a base64 HMAC SHA256 hash
https://gist.github.com/hanih/7443134
function sha256() {
var sha256;
sha256 = "undefined" !== typeof exports ? exports : {};
sha256.hexcase = 0;
sha256.b64pad = "=";
var b64pad = "=";
var hexcase = 0;
sha256.hex_sha256 = function(a) {
return sha256.rstr2hex(sha256.rstr_sha256(sha256.str2rstr_utf8(a)));
};
sha256.b64_sha256 = function(a) {
return sha256.rstr_sha256(sha256.str2rstr_utf8(a));
};
sha256.any_sha256 = function(a, c) {
return sha256.rstr2any(sha256.rstr_sha256(sha256.str2rstr_utf8(a)), c);
};
sha256.hex_hmac_sha256 = function(a, c) {
return sha256.rstr2hex(sha256.rstr_hmac_sha256(sha256.str2rstr_utf8(a), sha256.str2rstr_utf8(c)));
};
sha256.b64_hmac_sha256 = function(a, c) {
return sha256.rstr2b64(sha256.rstr_hmac_sha256(sha256.str2rstr_utf8(a), sha256.str2rstr_utf8(c)));
};
sha256.b64_hmac_sha256_sha256 = function(a, c) {
return sha256.rstr2b64(sha256.rstr_hmac_sha256(a, sha256.rstr_sha256(c)));
};
sha256.any_hmac_sha256 = function(a, c, b) {
return sha256.rstr2any(sha256.rstr_hmac_sha256(sha256.str2rstr_utf8(a), sha256.str2rstr_utf8(c)), b);
};
sha256.sha256_vm_test = function() {
return "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" == sha256.hex_sha256("abc").toLowerCase();
};
sha256.sha256_vm_test1 = function() {
return "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592" == sha256.hex_sha256("The quick brown fox jumps over the lazy dog").toLowerCase();
};
sha256.rstr_sha256 = function(a) {
return sha256.binb2rstr(sha256.binb_sha256(sha256.rstr2binb(a), 8 * a.length));
};
sha256.rstr_hmac_sha256 = function(a, c) {
var b = sha256.rstr2binb(a);
16 < b.length && ( b = sha256.binb_sha256(b, 8 * a.length));
for (var d = Array(16), e = Array(16), f = 0; 16 > f; f++)
d[f] = b[f] ^ 909522486, e[f] = b[f] ^ 1549556828;
b = sha256.binb_sha256(d.concat(sha256.rstr2binb(c)), 512 + 8 * c.length);
return sha256.binb2rstr(sha256.binb_sha256(e.concat(b), 768));
};
sha256.rstr2hex = function(a) {
try { hexcase;
} catch(c) {
hexcase = 0;
}
for (var b = hexcase ? "0123456789ABCDEF" : "0123456789abcdef", d = "", e, f = 0; f < a.length; f++)
e = a.charCodeAt(f), d += b.charAt(e >>> 4 & 15) + b.charAt(e & 15);
return d;
};
sha256.rstr2b64 = function(a) {
try { b64pad;
} catch(c) {
b64pad = "";
}
for (var b = "", d = a.length, e = 0; e < d; e += 3)
for (var f = a.charCodeAt(e) << 16 | (e + 1 < d ? a.charCodeAt(e + 1) << 8 : 0) | (e + 2 < d ? a.charCodeAt(e + 2) : 0), g = 0; 4 > g; g++)
b = 8 * e + 6 * g > 8 * a.length ? b + b64pad : b + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(f >>> 6 * (3 - g) & 63);
return b;
};
sha256.rstr2any = function(a, c) {
var b = c.length, d = [], e, f, g, h, j = Array(Math.ceil(a.length / 2));
for ( e = 0; e < j.length; e++)
j[e] = a.charCodeAt(2 * e) << 8 | a.charCodeAt(2 * e + 1);
for (; 0 < j.length; ) {
h = [];
for ( e = g = 0; e < j.length; e++)
if ( g = (g << 16) + j[e], f = Math.floor(g / b), g -= f * b, 0 < h.length || 0 < f)
h[h.length] = f;
d[d.length] = g;
j = h;
}
b = "";
for ( e = d.length - 1; 0 <= e; e--)
b += c.charAt(d[e]);
d = Math.ceil(8 * a.length / (Math.log(c.length) / Math.log(2)));
for ( e = b.length; e < d; e++)
b = c[0] + b;
return b;
};
sha256.str2rstr_utf8 = function(a) {
for (var c = "", b = -1, d, e; ++b < a.length; )
d = a.charCodeAt(b), e = b + 1 < a.length ? a.charCodeAt(b + 1) : 0, 55296 <= d && 56319 >= d && 56320 <= e && 57343 >= e && ( d = 65536 + ((d & 1023) << 10) + (e & 1023), b++), 127 >= d ? c += String.fromCharCode(d) : 2047 >= d ? c += String.fromCharCode(192 | d >>> 6 & 31, 128 | d & 63) : 65535 >= d ? c += String.fromCharCode(224 | d >>> 12 & 15, 128 | d >>> 6 & 63, 128 | d & 63) : 2097151 >= d && (c += String.fromCharCode(240 | d >>> 18 & 7, 128 | d >>> 12 & 63, 128 | d >>> 6 & 63, 128 | d & 63));
return c;
};
sha256.str2rstr_utf16le = function(a) {
for (var c = "", b = 0; b < a.length; b++)
c += String.fromCharCode(a.charCodeAt(b) & 255, a.charCodeAt(b) >>> 8 & 255);
return c;
};
str2rstr_utf16be = function(a) {
for (var c = "", b = 0; b < a.length; b++)
c += String.fromCharCode(a.charCodeAt(b) >>> 8 & 255, a.charCodeAt(b) & 255);
return c;
};
sha256.rstr2binb = function(a) {
for (var c = Array(a.length >> 2), b = 0; b < c.length; b++)
c[b] = 0;
for ( b = 0; b < 8 * a.length; b += 8)
c[b >> 5] |= (a.charCodeAt(b / 8) & 255) << 24 - b % 32;
return c;
};
sha256.binb2rstr = function(a) {
for (var c = "", b = 0; b < 32 * a.length; b += 8)
c += String.fromCharCode(a[b >> 5] >>> 24 - b % 32 & 255);
return c;
};
sha256.sha256_S = function(a, c) {
return a >>> c | a << 32 - c;
};
sha256.sha256_R = function(a, c) {
return a >>> c;
};
sha256.sha256_Ch = function(a, c, b) {
return a & c ^ ~a & b;
};
sha256.sha256_Maj = function(a, c, b) {
return a & c ^ a & b ^ c & b;
};
sha256.sha256_Sigma0256 = function(a) {
return sha256.sha256_S(a, 2) ^ sha256.sha256_S(a, 13) ^ sha256.sha256_S(a, 22);
};
sha256.sha256_Sigma1256 = function(a) {
return sha256.sha256_S(a, 6) ^ sha256.sha256_S(a, 11) ^ sha256.sha256_S(a, 25);
};
sha256.sha256_Gamma0256 = function(a) {
return sha256.sha256_S(a, 7) ^ sha256.sha256_S(a, 18) ^ sha256.sha256_R(a, 3);
};
sha256.sha256_Gamma1256 = function(a) {
return sha256.sha256_S(a, 17) ^ sha256.sha256_S(a, 19) ^ sha256.sha256_R(a, 10);
};
sha256.sha256_Sigma0512 = function(a) {
return sha256.sha256_S(a, 28) ^ sha256.sha256_S(a, 34) ^ sha256.sha256_S(a, 39);
};
sha256.sha256_Sigma1512 = function(a) {
return sha256.sha256_S(a, 14) ^ sha256.sha256_S(a, 18) ^ sha256.sha256_S(a, 41);
};
sha256.sha256_Gamma0512 = function(a) {
return sha256.sha256_S(a, 1) ^ sha256.sha256_S(a, 8) ^ sha256.sha256_R(a, 7);
};
sha256.sha256_Gamma1512 = function(a) {
return sha256.sha256_S(a, 19) ^ sha256.sha256_S(a, 61) ^ sha256.sha256_R(a, 6);
};
sha256.sha256_K = [1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998];
sha256.binb_sha256 = function(a, c) {
var b = [1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225], d = Array(64), e, f, g, h, j, k, m, o, p, n, l, q;
a[c >> 5] |= 128 << 24 - c % 32;
a[(c + 64 >> 9 << 4) + 15] = c;
for ( p = 0; p < a.length; p += 16) {
e = b[0];
f = b[1];
g = b[2];
h = b[3];
j = b[4];
k = b[5];
m = b[6];
o = b[7];
for ( n = 0; 64 > n; n++)
d[n] = 16 > n ? a[n + p] : sha256.safe_add(sha256.safe_add(sha256.safe_add(sha256.sha256_Gamma1256(d[n - 2]), d[n - 7]), sha256.sha256_Gamma0256(d[n - 15])), d[n - 16]), l = sha256.safe_add(sha256.safe_add(sha256.safe_add(sha256.safe_add(o, sha256.sha256_Sigma1256(j)), sha256.sha256_Ch(j, k, m)), sha256.sha256_K[n]), d[n]), q = sha256.safe_add(sha256.sha256_Sigma0256(e), sha256.sha256_Maj(e, f, g)), o = m, m = k, k = j, j = sha256.safe_add(h, l), h = g, g = f, f = e, e = sha256.safe_add(l, q);
b[0] = sha256.safe_add(e, b[0]);
b[1] = sha256.safe_add(f, b[1]);
b[2] = sha256.safe_add(g, b[2]);
b[3] = sha256.safe_add(h, b[3]);
b[4] = sha256.safe_add(j, b[4]);
b[5] = sha256.safe_add(k, b[5]);
b[6] = sha256.safe_add(m, b[6]);
b[7] = sha256.safe_add(o, b[7]);
}
return b;
};
sha256.safe_add = function(a, c) {
var b = (a & 65535) + (c & 65535);
return (a >> 16) + (c >> 16) + (b >> 16) << 16 | b & 65535;
};
return sha256;
}
module.exports = sha256;
it contains a function named b64_hmac_sha256 that works fine.
I use a base 64 encoded key that I decode using the following function:
https://gist.github.com/hanih/7443203
function urlDecode(str){
str=str.replace(new RegExp('\\+','g'),' ');
return unescape(str);
}
function urlEncode(str){
str=escape(str);
str=str.replace(new RegExp('\\+','g'),'%2B');
return str.replace(new RegExp('%20','g'),'+');
}
var END_OF_INPUT = -1;
var base64Chars = new Array(
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'
);
var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
reverseBase64Chars[base64Chars[i]] = i;
}
var base64Str;
var base64Count;
function setBase64Str(str){
base64Str = str;
base64Count = 0;
}
function readBase64(){
if (!base64Str) return END_OF_INPUT;
if (base64Count >= base64Str.length) return END_OF_INPUT;
var c = base64Str.charCodeAt(base64Count) & 0xff;
base64Count++;
return c;
}
function encodeBase64(str){
setBase64Str(str);
var result = '';
var inBuffer = new Array(3);
var lineCount = 0;
var done = false;
while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
inBuffer[1] = readBase64();
inBuffer[2] = readBase64();
result += (base64Chars[ inBuffer[0] >> 2 ]);
if (inBuffer[1] != END_OF_INPUT){
result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
if (inBuffer[2] != END_OF_INPUT){
result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
result += (base64Chars [inBuffer[2] & 0x3F]);
} else {
result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
result += ('=');
done = true;
}
} else {
result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
result += ('=');
result += ('=');
done = true;
}
lineCount += 4;
if (lineCount >= 76){
result += ('\n');
lineCount = 0;
}
}
return result;
}
function readReverseBase64(){
if (!base64Str) return END_OF_INPUT;
while (true){
if (base64Count >= base64Str.length) return END_OF_INPUT;
var nextCharacter = base64Str.charAt(base64Count);
base64Count++;
if (reverseBase64Chars[nextCharacter]){
return reverseBase64Chars[nextCharacter];
}
if (nextCharacter == 'A') return 0;
}
return END_OF_INPUT;
}
function ntos(n){
n=n.toString(16);
if (n.length == 1) n="0"+n;
n="%"+n;
return unescape(n);
}
function decodeBase64(str){
setBase64Str(str);
var result = "";
var inBuffer = new Array(4);
var done = false;
while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
&& (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
inBuffer[2] = readReverseBase64();
inBuffer[3] = readReverseBase64();
result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
if (inBuffer[2] != END_OF_INPUT){
result += ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
if (inBuffer[3] != END_OF_INPUT){
result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
} else {
done = true;
}
} else {
done = true;
}
}
return result;
}
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
function toHex(n){
var result = ''
var start = true;
for (var i=32; i>0;){
i-=4;
var digit = (n>>i) & 0xf;
if (!start || digit != 0){
start = false;
result += digitArray[digit];
}
}
return (result==''?'0':result);
}
function pad(str, len, pad){
var result = str;
for (var i=str.length; i<len; i++){
result = pad + result;
}
return result;
}
function encodeHex(str){
var result = "";
for (var i=0; i<str.length; i++){
result += pad(toHex(str.charCodeAt(i)&0xff),2,'0');
}
return result;
}
var hexv = {
"00":0,"01":1,"02":2,"03":3,"04":4,"05":5,"06":6,"07":7,"08":8,"09":9,"0A":10,"0B":11,"0C":12,"0D":13,"0E":14,"0F":15,
"10":16,"11":17,"12":18,"13":19,"14":20,"15":21,"16":22,"17":23,"18":24,"19":25,"1A":26,"1B":27,"1C":28,"1D":29,"1E":30,"1F":31,
"20":32,"21":33,"22":34,"23":35,"24":36,"25":37,"26":38,"27":39,"28":40,"29":41,"2A":42,"2B":43,"2C":44,"2D":45,"2E":46,"2F":47,
"30":48,"31":49,"32":50,"33":51,"34":52,"35":53,"36":54,"37":55,"38":56,"39":57,"3A":58,"3B":59,"3C":60,"3D":61,"3E":62,"3F":63,
"40":64,"41":65,"42":66,"43":67,"44":68,"45":69,"46":70,"47":71,"48":72,"49":73,"4A":74,"4B":75,"4C":76,"4D":77,"4E":78,"4F":79,
"50":80,"51":81,"52":82,"53":83,"54":84,"55":85,"56":86,"57":87,"58":88,"59":89,"5A":90,"5B":91,"5C":92,"5D":93,"5E":94,"5F":95,
"60":96,"61":97,"62":98,"63":99,"64":100,"65":101,"66":102,"67":103,"68":104,"69":105,"6A":106,"6B":107,"6C":108,"6D":109,"6E":110,"6F":111,
"70":112,"71":113,"72":114,"73":115,"74":116,"75":117,"76":118,"77":119,"78":120,"79":121,"7A":122,"7B":123,"7C":124,"7D":125,"7E":126,"7F":127,
"80":128,"81":129,"82":130,"83":131,"84":132,"85":133,"86":134,"87":135,"88":136,"89":137,"8A":138,"8B":139,"8C":140,"8D":141,"8E":142,"8F":143,
"90":144,"91":145,"92":146,"93":147,"94":148,"95":149,"96":150,"97":151,"98":152,"99":153,"9A":154,"9B":155,"9C":156,"9D":157,"9E":158,"9F":159,
"A0":160,"A1":161,"A2":162,"A3":163,"A4":164,"A5":165,"A6":166,"A7":167,"A8":168,"A9":169,"AA":170,"AB":171,"AC":172,"AD":173,"AE":174,"AF":175,
"B0":176,"B1":177,"B2":178,"B3":179,"B4":180,"B5":181,"B6":182,"B7":183,"B8":184,"B9":185,"BA":186,"BB":187,"BC":188,"BD":189,"BE":190,"BF":191,
"C0":192,"C1":193,"C2":194,"C3":195,"C4":196,"C5":197,"C6":198,"C7":199,"C8":200,"C9":201,"CA":202,"CB":203,"CC":204,"CD":205,"CE":206,"CF":207,
"D0":208,"D1":209,"D2":210,"D3":211,"D4":212,"D5":213,"D6":214,"D7":215,"D8":216,"D9":217,"DA":218,"DB":219,"DC":220,"DD":221,"DE":222,"DF":223,
"E0":224,"E1":225,"E2":226,"E3":227,"E4":228,"E5":229,"E6":230,"E7":231,"E8":232,"E9":233,"EA":234,"EB":235,"EC":236,"ED":237,"EE":238,"EF":239,
"F0":240,"F1":241,"F2":242,"F3":243,"F4":244,"F5":245,"F6":246,"F7":247,"F8":248,"F9":249,"FA":250,"FB":251,"FC":252,"FD":253,"FE":254,"FF":255
};
function decodeHex(str){
str = str.toUpperCase().replace(new RegExp("s/[^0-9A-Z]//g"));
var result = "";
var nextchar = "";
for (var i=0; i<str.length; i++){
nextchar += str.charAt(i);
if (nextchar.length == 2){
result += ntos(hexv[nextchar]);
nextchar = "";
}
}
return result;
}
The problem happens when the decoded key contains uncommon characters
This encoded key works:
dGhpc2lzYWxvbmdlcm1lc3NhZ2VvZmNvdXJzZXRoaXNpc2Fsb25nZXJtZXNzYWdlb2Zjb3Vyc2U=
but this doesn't work
5VoyMfmtN7lBiFlyDcMX85Hjvw/oxj8IVcB0dn8N6CXr+F0nuPI2LQ3K/w==

sha1 function in Javascript to python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How to convert the following js to python?
function sha1(str1,raw)
{
var hexcase = 0;
var chrsz = 8;
str1 = utf16to8(str1);
function utf16to8(str)
{
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i < len; i++)
{
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F))
{
out += str.charAt(i);
}
else if (c > 0x07FF)
{
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
else
{
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
function hex_sha1(s)
{
return binb2hex(core_sha1(str2binb(s),s.length * chrsz));
}
function str_sha1(s)
{
return binb2str(core_sha1(str2binb(s),s.length * chrsz));
}
function binb2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
}
return str;
}
function binb2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
return str;
}
function str2binb(str2)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str2.length * chrsz; i += chrsz)
bin[i>>5] |= (str2.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
return bin;
}
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
function sha1_ft(t, b, c, d)
{
if(t < 20)
return (b & c) | ((~b) & d);
if(t < 40)
return b ^ c ^ d;
if(t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
function sha1_kt(t)
{
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
function core_sha1(x, len)
{
x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for(var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for(var j = 0; j < 80; j++)
{
if(j < 16)
w[j] = x[i + j];
else
w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
if (raw == true)
{
return str_sha1(str1);
}
else
{
return hex_sha1(str1);
}
}
The standard library hashlib module includes sha1:
>>> import hashlib
>>> h = hashlib.sha1("lkjlkjlkj")
>>> h.digest()
"N]\x96\xb3:a^6\xa9A}.\x92\xea\xf6\xaa\x19'b{"
>>> h.hexdigest()
'4e5d96b33a615e36a9417d2e92eaf6aa1927627b'

Get SHA1 checksum of byte array in JavaScript?

So I need to get a SHA1 hash of a byte array in javascript (Will be array of integer values 0-255), I can't seem to figure out how to achive this.
I need to be able to get the same result as the C# SHA1.ComputeHash function, meaning I input a byte array and get a 20 byte array back representing the resulting checksum.
Does anyone have a way to achieve this?
Thanks!
Use the calcSHA1FromByte function to get the sha1 of a byte array
var SHA1Generator = {
hex_chr: "0123456789abcdef",
hex: function (num) {
var str = "";
for (var j = 7; j >= 0; j--)
str += this.hex_chr.charAt((num >> (j * 4)) & 0x0F);
return str;
},
str2blks_SHA1: function (str) {
var nblk = ((str.length + 8) >> 6) + 1;
var blks = new Array(nblk * 16);
for (var i = 0; i < nblk * 16; i++) blks[i] = 0;
for (i = 0; i < str.length; i++)
blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);
blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
blks[nblk * 16 - 1] = str.length * 8;
return blks;
},
add: function (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
},
rol: function (num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
},
ft: function (t, b, c, d) {
if (t < 20) return (b & c) | ((~b) & d);
if (t < 40) return b ^ c ^ d;
if (t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
},
kt: function (t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
},
calcSHA1FromByte: function(byteArr) {
var str = '';
for(var i=0; i<byteArr.length; i++)
str += String.fromCharCode(byteArr[i]);
return this.calcSHA1(str);
},
calcSHA1: function (str) {
if (str != '') {
var x = this.str2blks_SHA1(str);
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = x[i + j];
else w[j] = this.rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
t = this.add(this.add(this.rol(a, 5), this.ft(j, b, c, d)), this.add(this.add(e, w[j]), this.kt(j)));
e = d;
d = c;
c = this.rol(b, 30);
b = a;
a = t;
}
a = this.add(a, olda);
b = this.add(b, oldb);
c = this.add(c, oldc);
d = this.add(d, oldd);
e = this.add(e, olde);
}
return this.hex(a) + this.hex(b) + this.hex(c) + this.hex(d) + this.hex(e);
}
else {
return '';
}
}
};
// your byte array
var byteArr = ['100','101','114'];
var sha1 = SHA1Generator.calcSHA1FromByte(byteArr);

Categories