I implemented a sha-256 hash function in javascript. The problem is that my hash function doesn't output the correct hash.
Here's my implementation code:
var _k = [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]
var _h = [0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19];
function addFrontZero(str, fixedlength){
while(str.length < fixedlength){
str = 0 + str;
}
return str;
}
function rotr(n,x){
return ((x >> n) | (x << 32 - n));
}
function ch(x,y,z){
return (x & y) ^ (~x & z);
}
function maj(x,y,z){
return (x & y) ^ (x & z) ^ (y & z);
}
function sigma0(x){
return rotr(2,x) ^ rotr(13,x) ^ rotr(22,x);
}
function sigma1(x){
return rotr(6,x) ^ rotr(11,x) ^ rotr(25,x);
}
function omega0(x){
return rotr(7,x) ^ rotr(18,x) ^ (x >> 3);
}
function omega1(x){
return rotr(17,x) ^ rotr(19,x) ^ (x >> 10);
}
function sha256(string){
var H = _h.slice();
var K = _k.slice();
var str =[];
for(var i=0; i<string.length; i++){
var t = (string.charCodeAt(i)).toString(2);
t = (t.length < 8) ? addFrontZero(t,8) : t;
str.push(t);
}
str = str.join("");
var zeroBits = addFrontZero("",448-(str.length+1));
var lengthBits = addFrontZero((str.length).toString(2), 64);
str = str + "1" + zeroBits + lengthBits;
var M = [];
for(var i=0; i<512; i+=32){
M.push(str.substr(i,32))
}
var W = [];
var temp;
for(var i=0; i<16; i++){
W.push(parseInt(M[i], 2) % Math.pow(2,32));
}
for(var i=16; i<64; i++){
temp = (omega1(W[i-2]) + W[i-7] + omega0(W[i-15]) + W[i-16]) % Math.pow(2,32);
W.push(temp);
}
var a, b, c, d, e, f, g, h, t1, t2;
for(var i=0; i<M.length; i++){
a = H[0];
b = H[1];
c = H[2];
d = H[3];
e = H[4];
f = H[5];
g = H[6];
h = H[7];
for(var t=0; t<64; t++){
t1 = (h + sigma1(e) + ch(e,f,g) + K[t] + W[t]) % Math.pow(2,32);
t2 = (sigma0(a) + maj(a,b,c)) % Math.pow(2,32);
h = g;
g = f;
f = e;
e = (d + t1) % Math.pow(2,32);
d = c;
c = b;
b = a;
a = (t1 + t2) % Math.pow(2,32);
}
H[0] = (a + H[0]) % Math.pow(2,32);
H[1] = (b + H[1]) % Math.pow(2,32);
H[2] = (c + H[2]) % Math.pow(2,32);
H[3] = (d + H[3]) % Math.pow(2,32);
H[4] = (e + H[4]) % Math.pow(2,32);
H[5] = (f + H[5]) % Math.pow(2,32);
H[6] = (g + H[6]) % Math.pow(2,32);
H[7] = (h + H[7]) % Math.pow(2,32);
}
var output = "";
for(var i=0; i<8; i++){
var temp = (H[i] >>> 0).toString(16);
output += (addFrontZero(temp, 8));
}
return output;
}
What I have tried:
I try checking all for typo's but I cant see what wrong in my implementation.