mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 01:45:15 +00:00
96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
|
// Adapted from Chris Veness' SHA1 code at
|
||
|
// http://www.movable-type.co.uk/scripts/sha1.html
|
||
|
function f(s, x, y, z) {
|
||
|
switch (s) {
|
||
|
case 0:
|
||
|
return x & y ^ ~x & z;
|
||
|
|
||
|
case 1:
|
||
|
return x ^ y ^ z;
|
||
|
|
||
|
case 2:
|
||
|
return x & y ^ x & z ^ y & z;
|
||
|
|
||
|
case 3:
|
||
|
return x ^ y ^ z;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function ROTL(x, n) {
|
||
|
return x << n | x >>> 32 - n;
|
||
|
}
|
||
|
|
||
|
function sha1(bytes) {
|
||
|
var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
|
||
|
var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
|
||
|
|
||
|
if (typeof bytes === 'string') {
|
||
|
var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
|
||
|
|
||
|
bytes = [];
|
||
|
|
||
|
for (var i = 0; i < msg.length; ++i) {
|
||
|
bytes.push(msg.charCodeAt(i));
|
||
|
}
|
||
|
} else if (!Array.isArray(bytes)) {
|
||
|
// Convert Array-like to Array
|
||
|
bytes = Array.prototype.slice.call(bytes);
|
||
|
}
|
||
|
|
||
|
bytes.push(0x80);
|
||
|
var l = bytes.length / 4 + 2;
|
||
|
var N = Math.ceil(l / 16);
|
||
|
var M = new Array(N);
|
||
|
|
||
|
for (var _i = 0; _i < N; ++_i) {
|
||
|
var arr = new Uint32Array(16);
|
||
|
|
||
|
for (var j = 0; j < 16; ++j) {
|
||
|
arr[j] = bytes[_i * 64 + j * 4] << 24 | bytes[_i * 64 + j * 4 + 1] << 16 | bytes[_i * 64 + j * 4 + 2] << 8 | bytes[_i * 64 + j * 4 + 3];
|
||
|
}
|
||
|
|
||
|
M[_i] = arr;
|
||
|
}
|
||
|
|
||
|
M[N - 1][14] = (bytes.length - 1) * 8 / Math.pow(2, 32);
|
||
|
M[N - 1][14] = Math.floor(M[N - 1][14]);
|
||
|
M[N - 1][15] = (bytes.length - 1) * 8 & 0xffffffff;
|
||
|
|
||
|
for (var _i2 = 0; _i2 < N; ++_i2) {
|
||
|
var W = new Uint32Array(80);
|
||
|
|
||
|
for (var t = 0; t < 16; ++t) {
|
||
|
W[t] = M[_i2][t];
|
||
|
}
|
||
|
|
||
|
for (var _t = 16; _t < 80; ++_t) {
|
||
|
W[_t] = ROTL(W[_t - 3] ^ W[_t - 8] ^ W[_t - 14] ^ W[_t - 16], 1);
|
||
|
}
|
||
|
|
||
|
var a = H[0];
|
||
|
var b = H[1];
|
||
|
var c = H[2];
|
||
|
var d = H[3];
|
||
|
var e = H[4];
|
||
|
|
||
|
for (var _t2 = 0; _t2 < 80; ++_t2) {
|
||
|
var s = Math.floor(_t2 / 20);
|
||
|
var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[_t2] >>> 0;
|
||
|
e = d;
|
||
|
d = c;
|
||
|
c = ROTL(b, 30) >>> 0;
|
||
|
b = a;
|
||
|
a = T;
|
||
|
}
|
||
|
|
||
|
H[0] = H[0] + a >>> 0;
|
||
|
H[1] = H[1] + b >>> 0;
|
||
|
H[2] = H[2] + c >>> 0;
|
||
|
H[3] = H[3] + d >>> 0;
|
||
|
H[4] = H[4] + e >>> 0;
|
||
|
}
|
||
|
|
||
|
return [H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff, H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff, H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff, H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff, H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff];
|
||
|
}
|
||
|
|
||
|
export default sha1;
|