11 #define _CRT_SECURE_NO_WARNINGS
14 #define SHA1_MAX_FILE_BUFFER (32 * 20 * 820)
19 #define ROL32(p_val32,p_nBits) _rotl(p_val32,p_nBits)
21 #define ROL32(p_val32,p_nBits) (((p_val32)<<(p_nBits))|((p_val32)>>(32-(p_nBits))))
25 #ifdef SHA1_LITTLE_ENDIAN
26 #define SHABLK0(i) (m_block->l[i] = \
27 (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
29 #define SHABLK0(i) (m_block->l[i])
32 #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ \
33 m_block->l[(i+8)&15] ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
36 #define S_R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
37 #define S_R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
38 #define S_R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);}
39 #define S_R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5);w=ROL32(w,30);}
40 #define S_R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);}
44 #pragma warning(disable: 4127)
53 #ifdef SHA1_WIPE_VARIABLES
75 UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4];
80 S_R0(a,b,c,d,e, 0);
S_R0(e,a,b,c,d, 1);
S_R0(d,e,a,b,c, 2);
S_R0(c,d,e,a,b, 3);
81 S_R0(b,c,d,e,a, 4);
S_R0(a,b,c,d,e, 5);
S_R0(e,a,b,c,d, 6);
S_R0(d,e,a,b,c, 7);
82 S_R0(c,d,e,a,b, 8);
S_R0(b,c,d,e,a, 9);
S_R0(a,b,c,d,e,10);
S_R0(e,a,b,c,d,11);
83 S_R0(d,e,a,b,c,12);
S_R0(c,d,e,a,b,13);
S_R0(b,c,d,e,a,14);
S_R0(a,b,c,d,e,15);
84 S_R1(e,a,b,c,d,16);
S_R1(d,e,a,b,c,17);
S_R1(c,d,e,a,b,18);
S_R1(b,c,d,e,a,19);
85 S_R2(a,b,c,d,e,20);
S_R2(e,a,b,c,d,21);
S_R2(d,e,a,b,c,22);
S_R2(c,d,e,a,b,23);
86 S_R2(b,c,d,e,a,24);
S_R2(a,b,c,d,e,25);
S_R2(e,a,b,c,d,26);
S_R2(d,e,a,b,c,27);
87 S_R2(c,d,e,a,b,28);
S_R2(b,c,d,e,a,29);
S_R2(a,b,c,d,e,30);
S_R2(e,a,b,c,d,31);
88 S_R2(d,e,a,b,c,32);
S_R2(c,d,e,a,b,33);
S_R2(b,c,d,e,a,34);
S_R2(a,b,c,d,e,35);
89 S_R2(e,a,b,c,d,36);
S_R2(d,e,a,b,c,37);
S_R2(c,d,e,a,b,38);
S_R2(b,c,d,e,a,39);
90 S_R3(a,b,c,d,e,40);
S_R3(e,a,b,c,d,41);
S_R3(d,e,a,b,c,42);
S_R3(c,d,e,a,b,43);
91 S_R3(b,c,d,e,a,44);
S_R3(a,b,c,d,e,45);
S_R3(e,a,b,c,d,46);
S_R3(d,e,a,b,c,47);
92 S_R3(c,d,e,a,b,48);
S_R3(b,c,d,e,a,49);
S_R3(a,b,c,d,e,50);
S_R3(e,a,b,c,d,51);
93 S_R3(d,e,a,b,c,52);
S_R3(c,d,e,a,b,53);
S_R3(b,c,d,e,a,54);
S_R3(a,b,c,d,e,55);
94 S_R3(e,a,b,c,d,56);
S_R3(d,e,a,b,c,57);
S_R3(c,d,e,a,b,58);
S_R3(b,c,d,e,a,59);
95 S_R4(a,b,c,d,e,60);
S_R4(e,a,b,c,d,61);
S_R4(d,e,a,b,c,62);
S_R4(c,d,e,a,b,63);
96 S_R4(b,c,d,e,a,64);
S_R4(a,b,c,d,e,65);
S_R4(e,a,b,c,d,66);
S_R4(d,e,a,b,c,67);
97 S_R4(c,d,e,a,b,68);
S_R4(b,c,d,e,a,69);
S_R4(a,b,c,d,e,70);
S_R4(e,a,b,c,d,71);
98 S_R4(d,e,a,b,c,72);
S_R4(c,d,e,a,b,73);
S_R4(b,c,d,e,a,74);
S_R4(a,b,c,d,e,75);
99 S_R4(e,a,b,c,d,76);
S_R4(d,e,a,b,c,77);
S_R4(c,d,e,a,b,78);
S_R4(b,c,d,e,a,79);
109 #ifdef SHA1_WIPE_VARIABLES
110 a = b = c = d = e = 0;
118 if((
m_count[0] += (uLen << 3)) < (uLen << 3))
130 for( ; (i + 63) < uLen; i += 64)
138 memcpy(&
m_buffer[j], &pbData[i], uLen - i);
141 #ifdef SHA1_UTILITY_FUNCTIONS
144 if(tszFileName == NULL)
return false;
146 FILE* fpIn =
_tfopen(tszFileName,
_T(
"rb"));
147 if(fpIn == NULL)
return false;
150 if(pbData == NULL) { fclose(fpIn);
return false; }
152 bool bSuccess =
true;
158 Update(pbData, static_cast<UINT_32>(uRead));
162 if(feof(fpIn) == 0) bSuccess =
false;
178 for(i = 0; i < 8; ++i)
179 pbFinalCount[i] = static_cast<UINT_8>((
m_count[((i >= 4) ? 0 : 1)] >>
180 ((3 - (i & 3)) * 8) ) & 0xFF);
184 while((
m_count[0] & 504) != 448)
189 for(i = 0; i < 20; ++i)
191 (i & 3)) * 8)) & 0xFF);
194 #ifdef SHA1_WIPE_VARIABLES
198 memset(pbFinalCount, 0, 8);
203 #ifdef SHA1_UTILITY_FUNCTIONS
206 if(tszReport == NULL)
return false;
216 for(
size_t i = 1; i < 20; ++i)
227 for(
size_t i = 1; i < 20; ++i)
239 #ifdef SHA1_STL_FUNCTIONS
243 const bool bResult =
ReportHash(tszOut, rtReportType);
244 if(bResult) strOut = tszOut;
251 if(pbDest20 == NULL)
return false;