Last active
August 29, 2015 13:56
-
-
Save yurydelendik/9084184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf | |
var W = new Int32Array(80); | |
function sha1(s) { | |
// 6.1.1 SHA-1 Preprocessing | |
var H = new Int32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]); | |
// 5.1 Padding the Message | |
var sLength = s.length, sLength8 = sLength << 3; | |
var length = (((sLength8 + 64) | 511) + 1) >> 3; | |
var m = new Uint8Array(length); | |
for (var i = 0; i < sLength; i++) { | |
m[i] = s.charCodeAt(i); | |
} | |
m[sLength] = 0x80; | |
m[length - 4] = (sLength8 >> 24) & 255; | |
m[length - 3] = (sLength8 >> 16) & 255; | |
m[length - 2] = (sLength8 >> 8) & 255; | |
m[length - 1] = sLength8 & 255; | |
for (var i = 0; i < length;) { | |
// 1. Prepare the message schedule, {Wt}: | |
for (var t = 0; t < 16; t++) { | |
var M = (m[i] << 24) | (m[i + 1] << 16) | (m[i + 2] << 8) | m[i + 3]; | |
W[t] = M; | |
i += 4; | |
} | |
for (var t = 16; t < 80; t++) { | |
var T = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]; | |
W[t] = (T << 1) | (T >>> 31); | |
} | |
// 2. Initialize the five working variables, a, b, c, d, and e, with the (i-1)^st hash value: | |
var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4]; | |
// 3. | |
for (var t = 0; t < 20; t++) { | |
var T = (((a << 5) | (a >>> 27)) + ((b & c) ^ (~b & d)) + e + 0x5a827999 + W[t]) | 0; | |
e = d; | |
d = c; | |
c = ((b << 30) | (b >>> 2)); | |
b = a; | |
a = T; | |
} | |
for (var t = 20; t < 40; t++) { | |
var T = (((a << 5) | (a >>> 27)) + (b ^ c ^ d) + e + 0x6ed9eba1 + W[t]) | 0; | |
e = d; | |
d = c; | |
c = ((b << 30) | (b >>> 2)); | |
b = a; | |
a = T; | |
} | |
for (var t = 40; t < 60; t++) { | |
var T = (((a << 5) | (a >>> 27)) + ((b & c) ^ (b & d) ^ (c & d)) + e + 0x8f1bbcdc + W[t]) | 0; | |
e = d; | |
d = c; | |
c = ((b << 30) | (b >>> 2)); | |
b = a; | |
a = T; | |
} | |
for (var t = 60; t < 80; t++) { | |
var T = (((a << 5) | (a >>> 27)) + (b ^ c ^ d) + e + 0xca62c1d6 + W[t]) | 0; | |
e = d; | |
d = c; | |
c = ((b << 30) | (b >>> 2)); | |
b = a; | |
a = T; | |
} | |
// 4.Compute the i-th intermediate hash value H(i) | |
H[0] = (a + H[0]) | 0; | |
H[1] = (b + H[1]) | 0; | |
H[2] = (c + H[2]) | 0; | |
H[3] = (d + H[3]) | 0; | |
H[4] = (e + H[4]) | 0; | |
} | |
return H; | |
} | |
function toHexString(h) { | |
return Array.prototype.map.call(h, function (i) { return (i >>> 0).toString(16); }).join(' '); | |
} | |
// http://www.nsrl.nist.gov/testdata/ | |
if (toHexString(sha1("abc")) !== "a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d") throw "failed 1"; | |
if (toHexString(sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")) !== "84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1") throw "failed 1"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf | |
var W = new Int32Array(64); | |
function sha256(s) { | |
// 6.2.1 SHA-256 Preprocessing | |
var H = new Int32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, | |
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]); | |
// 5.1 Padding the Message | |
var sLength = s.length, sLength8 = sLength << 3; | |
var length = (((sLength8 + 64) | 511) + 1) >> 3; | |
var m = new Uint8Array(length); | |
for (var i = 0; i < sLength; i++) { | |
m[i] = s.charCodeAt(i); | |
} | |
m[sLength] = 0x80; | |
m[length - 4] = (sLength8 >> 24) & 255; | |
m[length - 3] = (sLength8 >> 16) & 255; | |
m[length - 2] = (sLength8 >> 8) & 255; | |
m[length - 1] = sLength8 & 255; | |
for (var i = 0; i < length;) { | |
// 1. Prepare the message schedule, {Wt}: | |
for (var t = 0; t < 16; t++) { | |
var M = (m[i] << 24) | (m[i + 1] << 16) | (m[i + 2] << 8) | m[i + 3]; | |
W[t] = M; | |
i += 4; | |
} | |
for (var t = 16; t < 64; t++) { | |
var x1 = W[t - 2], x0 = W[t - 15]; | |
var s1 = ((x1 >>> 17) | (x1 << 15)) ^ ((x1 >>> 19) | (x1 << 13)) ^ (x1 >>> 10); | |
var s0 = ((x0 >>> 7) | (x0 << 25)) ^ ((x0 >>> 18) | (x0 << 14)) ^ (x0 >>> 3); | |
W[t] = (s1 + W[t - 7] + s0 + W[t - 16]) | 0; | |
} | |
// 2. Initialize the eight working variables, a, b, c, d, e, f, g, and h, | |
// with the (i-1)^st hash value: | |
var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7]; | |
// 3. | |
for (var t = 0; t < 64; t++) { | |
var S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7)); | |
var Ch = (e & f) ^ (~e & g); | |
var T1 = (h + S1 + Ch + K[t] + W[t]) | 0; | |
var S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10)); | |
var Maj = (a & b) ^ (a & c) ^ (b & c); | |
var T2 = (S0 + Maj) | 0; | |
h = g; | |
g = f; | |
f = e; | |
e = (d + T1) | 0; | |
d = c; | |
c = b; | |
b = a; | |
a = (T1 + T2) | 0; | |
} | |
// 4.Compute the i-th intermediate hash value H(i) | |
H[0] = (a + H[0]) | 0; | |
H[1] = (b + H[1]) | 0; | |
H[2] = (c + H[2]) | 0; | |
H[3] = (d + H[3]) | 0; | |
H[4] = (e + H[4]) | 0; | |
H[5] = (f + H[5]) | 0; | |
H[6] = (g + H[6]) | 0; | |
H[7] = (h + H[7]) | 0; | |
} | |
return H; | |
} | |
var K = new Int32Array([ | |
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 toHexString(h) { | |
return Array.prototype.map.call(h, function (i) { return (i >>> 0).toString(16).toUpperCase(); }).join(' '); | |
} | |
// http://www.nsrl.nist.gov/testdata/ | |
if (toHexString(sha256("abc")) !== "BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD") throw "failed 1"; | |
if (toHexString(sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")) !== "248D6A61 D20638B8 E5C02693 C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1") throw "failed 2"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment