Home > Back-end >  The SHA algorithm 1
The SHA algorithm 1

Time:09-22

For help, why this is the right code to calculate the sha - 1 values, where there is wrong, code:
 
Void SHA1: : Sha1Encode
(
_In_ const char * text,
_In_ size_t len,
_Inout_ unsigned char * outData
)
{
/*
* constants initialization
*/
Unsigned long k=0, f=0;
Unsigned long temp_ul_text [80].
Unsigned long temp_A=0;
Unsigned long h []={x98badcfe x67452301 0, 0 xefcdab89, 0, 0 x10325476, 0 xc3d2e1f0};
/*
* start filling the original
*/
Size_t n_len=((len + 8)/64) * 64 + 56 + 8;
Unsigned char * n_text=(unsigned char *) malloc (n_len);
Memset (n_text, 0 x00 n_len);
Memcpy (n_text, text, len);
N_text [len]=0 x80;
Unsigned char c_lens [8].
Memset (c_lens, 0 x00, 8);
Unsigned long temp_len=(unsigned long) (len * 8);
Memcpy (c_lens, & amp; Temp_len, sizeof (unsigned long));
Memcpy (n_text + (n_len - 8), c_lens, 8).
/*
//debug code to check after filling data
for (int i=0; i Printf (" % d ", n_text [I]); */

/*
* grouping calculation, 512 bits of a set of
*/
for (int i=0; i {
//A=H [0], B=H [1], C=H [2], [3] D=H, E=H [4]
Unsigned long H [5]=,0,0,0,0 {0};
Unsigned char temp_text [64].

Memset (temp_text, 0 x00, 64);
Memset (temp_ul_text, 0 x00, 80 * sizeof (unsigned long));
Memcpy (H, H, 5 * (sizeof (unsigned long)));
Memcpy (temp_text, (n_text + I), 64);

CharToUlong (temp_text temp_ul_text);

For (int j=0; J & lt; 80; J++)
{
The switch (20) (int) j/
{
Case 0:
K=0 x5a827999;
F=(H [1] & amp; [2]) | (H (H ~ [1]) & amp; H [3]);
break;
Case 1:
K=0 x6ed9eba1;
F=[1] H ^ H [2] ^ H [3].
break;
Case 2:
K=0 x8f1bbcdc;
F=(H [1] & amp; H [2]) | (H [1] & amp; H [3]) | (H [2] & amp; H [3]);
break;
Case 3:
K=0 xca62c1d6;
F=[1] H ^ H [2] ^ H [3].
break;
Default:
break;
}
Temp_A=((H [0] <5) | (H [0] & gt;> 27)) + temp_ul_text [j] f + H +, k + [4];
H [3] [4]=H;
H [3]=H [2];
[1] [2]=(H H & lt; <30) | (H [1] & gt;> 2);
H [1]=H [0];
H [0]=temp_A;
}
For (int k=0; K & lt; 5; K++)
H [k] +=h [k].
}
Free (n_text);
//debugging code, the output
For (int o=0; O & lt; 5; O++)
Printf (" % x, "h [o]);
}

The inline void SHA1: : CharToUlong
(
_In_ const unsigned char * char_data,
_Inout_ unsigned long * ulong_data
)
{
Memset (ulong_data, 0 x00, 80);
for(int i=0; i<16. I++)
Ulong_data [I]=(((unsigned long *) char_data) [I]);
For (int I=16; i <80; I++)
{
Ulong_data [I]=ulong_data [16] I - ^ ulong_data [14] I - ^
Ulong_data [8] I - ^ ulong_data [I - 3];
Ulong_data [I]=(ulong_data [I] <1) | (ulong_data [I] & gt;> 31);
}
}

  • Related