Hi, I'm trying to make the sha-1 algorithm based on the wikipedia pseudocode.
For the 'The quick brown fox jumps over the lazy dog" string I should get
2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12 but what I get is
1D8B8841 A544939A 98BB54FE 4C325476 C44AE1F0
(after I convert from dec to hex)
I have no idea what the problem is :(
Thx in advance
For the 'The quick brown fox jumps over the lazy dog" string I should get
2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12 but what I get is
1D8B8841 A544939A 98BB54FE 4C325476 C44AE1F0
(after I convert from dec to hex)
I have no idea what the problem is :(
Thx in advance
Code:
#define ROTL ( a , b ) ( ( ( b ) << a ) | ( ( b ) >> ( 32 - a ) ) ) void SHA1(char *in, long nr, char *sha) { long i, j, w[80]; unsigned long h0 = 0x67452301; unsigned long h1 = 0xEFCDAB89; unsigned long h2 = 0x98BADCFE; unsigned long h3 = 0x10325476; unsigned long h4 = 0xC3D2E1F0; unsigned long a, b, c, d, e, f, k, temp; unsigned long length = nr*32; in[nr++] = (char)128;//1 7x0 while(nr%64 != 60)in[nr++] = 0;//8x0 *((long*)(in+nr)) = length; nr += 4; for(i=0;i<nr;i+=64) { for(j=i;j<i+64;j+=4)w[(i-j)/4] = *((long*)in+j);// break chunk into sixteen 32-bit big-endian words w[i], 0 <= i <= 15 for(i=16;i<80;i++)w[i] = ROTL((w[i-3]^w[i-8]^w[i-14]^w[i-16]), 1); a = h0; b = h1; c = h2; d = h3; e = h4; for(i=0;i<80;i++) { if(0<=i&&i<20) { f = (b & c) | ((! b) & d); k = 0x5A827999; } else if(i>=20&&i<40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } else if(i>=40&&i<60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } else if(i>=60&&i<80) { f = b ^ c ^ d; k = 0xCA62C1D6; } temp = ROTL(a, 5) + f + e + k + w[i]; e = d; d = c; c = ROTL(b, 30); b = a; a = temp; } h0 = h0 + a; h1 = h1 + b; h2 = h2 + c; h3 = h3 + d; h4 = h4 + e; } sprintf_s(sha, 512, "%u %u %u %u %u", h0, h1, h2, h3, h4); }
Comment