SHA1.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. 100% free public domain implementation of the SHA-1
  3. algorithm by Dominik Reichl <Dominik.Reichl@swp-net.de>
  4. === Test Vectors (from FIPS PUB 180-1) ===
  5. "abc"
  6. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  7. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  8. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  9. A million repetitions of "a"
  10. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  11. */
  12. #include "SHA1.h"
  13. CSHA1::CSHA1()
  14. {
  15. Reset();
  16. }
  17. CSHA1::~CSHA1()
  18. {
  19. Reset();
  20. }
  21. void CSHA1::Reset()
  22. {
  23. // SHA1 initialization constants
  24. m_state[0] = 0x67452301;
  25. m_state[1] = 0xEFCDAB89;
  26. m_state[2] = 0x98BADCFE;
  27. m_state[3] = 0x10325476;
  28. m_state[4] = 0xC3D2E1F0;
  29. m_count[0] = 0;
  30. m_count[1] = 0;
  31. }
  32. void CSHA1::Transform(unsigned long state[5], unsigned char buffer[64])
  33. {
  34. unsigned long a = 0, b = 0, c = 0, d = 0, e = 0;
  35. SHA1_WORKSPACE_BLOCK* block;
  36. static unsigned char workspace[64];
  37. block = (SHA1_WORKSPACE_BLOCK*)workspace;
  38. memcpy(block, buffer, 64);
  39. // Copy state[] to working vars
  40. a = state[0];
  41. b = state[1];
  42. c = state[2];
  43. d = state[3];
  44. e = state[4];
  45. // 4 rounds of 20 operations each. Loop unrolled.
  46. R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  47. R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  48. R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  49. R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  50. R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  51. R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  52. R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  53. R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  54. R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  55. R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  56. R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  57. R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  58. R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  59. R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  60. R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  61. R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  62. R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  63. R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  64. R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  65. R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  66. // Add the working vars back into state[]
  67. state[0] += a;
  68. state[1] += b;
  69. state[2] += c;
  70. state[3] += d;
  71. state[4] += e;
  72. // Wipe variables
  73. a = 0; b = 0; c = 0; d = 0; e = 0;
  74. }
  75. // Use this function to hash in binary data and strings
  76. void CSHA1::Update(unsigned char* data, unsigned int len)
  77. {
  78. unsigned long i = 0, j = 0;
  79. j = (m_count[0] >> 3) & 63;
  80. if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
  81. m_count[1] += (len >> 29);
  82. if((j + len) > 63)
  83. {
  84. memcpy(&m_buffer[j], data, (i = 64 - j));
  85. Transform(m_state, m_buffer);
  86. for (; i+63 < len; i += 64)
  87. {
  88. Transform(m_state, &data[i]);
  89. }
  90. j = 0;
  91. }
  92. else i = 0;
  93. memcpy(&m_buffer[j], &data[i], len - i);
  94. }
  95. // Hash in file contents
  96. bool CSHA1::HashFile(char *szFileName)
  97. {
  98. unsigned long ulFileSize = 0, ulRest = 0, ulBlocks = 0;
  99. unsigned long i = 0;
  100. unsigned char uData[MAX_FILE_READ_BUFFER];
  101. FILE *fIn = NULL;
  102. if((fIn = fopen(szFileName, "rb")) == NULL) return(false);
  103. fseek(fIn, 0, SEEK_END);
  104. ulFileSize = ftell(fIn);
  105. fseek(fIn, 0, SEEK_SET);
  106. ulRest = ulFileSize % MAX_FILE_READ_BUFFER;
  107. ulBlocks = ulFileSize / MAX_FILE_READ_BUFFER;
  108. for(i = 0; i < ulBlocks; i++)
  109. {
  110. fread(uData, 1, MAX_FILE_READ_BUFFER, fIn);
  111. Update(uData, MAX_FILE_READ_BUFFER);
  112. }
  113. if(ulRest != 0)
  114. {
  115. fread(uData, 1, ulRest, fIn);
  116. Update(uData, ulRest);
  117. }
  118. fclose(fIn);
  119. fIn = NULL;
  120. return(true);
  121. }
  122. void CSHA1::Final()
  123. {
  124. unsigned long i = 0, j = 0;
  125. unsigned char finalcount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  126. for (i = 0; i < 8; i++)
  127. finalcount[i] = (unsigned char)((m_count[(i >= 4 ? 0 : 1)]
  128. >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
  129. Update((unsigned char *)"\200", 1);
  130. while ((m_count[0] & 504) != 448)
  131. Update((unsigned char *)"\0", 1);
  132. Update(finalcount, 8); // Cause a SHA1Transform()
  133. for (i = 0; i < 20; i++)
  134. {
  135. m_digest[i] = (unsigned char)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
  136. }
  137. // Wipe variables for security reasons
  138. i = 0; j = 0;
  139. memset(m_buffer, 0, 64);
  140. memset(m_state, 0, 20);
  141. memset(m_count, 0, 8);
  142. memset(finalcount, 0, 8);
  143. Transform(m_state, m_buffer);
  144. }
  145. // Get the final hash as a pre-formatted string
  146. void CSHA1::ReportHash(char *szReport, unsigned char uReportType)
  147. {
  148. unsigned char i = 0;
  149. if(uReportType == REPORT_HEX)
  150. {
  151. sprintf(szReport, "%02X", m_digest[0]);
  152. for(i = 1; i < 20; i++)
  153. {
  154. sprintf(szReport, "%s%02X", szReport, m_digest[i]);
  155. }
  156. }
  157. else if(uReportType == REPORT_DIGIT)
  158. {
  159. sprintf(szReport, "%u", m_digest[0]);
  160. for(i = 1; i < 20; i++)
  161. {
  162. sprintf(szReport, "%s %u", szReport, m_digest[i]);
  163. }
  164. }
  165. else sprintf(szReport, "Error: Unknown report type!");
  166. }
  167. // Get the raw message digest
  168. void CSHA1::GetHash(unsigned char *uDest)
  169. {
  170. unsigned char i = 0;
  171. for(i = 0; i < 20; i++)
  172. uDest[i] = m_digest[i];
  173. }