Main Page | Class List | File List | Class Members

sha1.h

00001 /*
00002         100% free public domain implementation of the SHA-1
00003         algorithm by Dominik Reichl <dominik.reichl@t-online.de>
00004 
00005 
00006         === Test Vectors (from FIPS PUB 180-1) ===
00007 
00008         SHA1("abc") =
00009                 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
00010 
00011         SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
00012                 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
00013 
00014         SHA1(A million repetitions of "a") =
00015                 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
00016 */
00017 
00018 #ifndef ___SHA1_H___
00019 #define ___SHA1_H___
00020 
00021 #include <stdio.h>  // Needed for file access
00022 #include <memory.h> // Needed for memset and memcpy
00023 #include <string.h> // Needed for strcat and strcpy
00024 
00025 #if 0
00026   #define LITTLE_ENDIAN
00027 #endif
00028 
00029 #define MAX_FILE_READ_BUFFER 8000
00030 
00031 class CSHA1
00032 {
00033 public:
00034         // Rotate x bits to the left
00035         #define ROL32(value, bits) (((value)<<(bits))|((value)>>(32-(bits))))
00036 
00037         #ifdef LITTLE_ENDIAN
00038                 #define SHABLK0(i) (m_block->l[i] = \
00039                         (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
00040         #else
00041                 #define SHABLK0(i) (m_block->l[i])
00042         #endif
00043 
00044         #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
00045                 ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
00046 
00047         // SHA-1 rounds
00048         #define R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
00049         #define R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
00050         #define R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
00051         #define R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
00052         #define R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
00053 
00054         typedef union
00055         {
00056                 unsigned char c[64];
00057                 unsigned long l[16];
00058         } SHA1_WORKSPACE_BLOCK;
00059 
00060         // Two different formats for ReportHash(...)
00061         enum
00062         {
00063                 REPORT_HEX = 0,
00064                 REPORT_DIGIT = 1
00065         };
00066 
00067         // Constructor and Destructor
00068         CSHA1();
00069         virtual ~CSHA1();
00070 
00071         unsigned long m_state[5];
00072         unsigned long m_count[2];
00073         unsigned char m_buffer[64];
00074         unsigned char m_digest[20];
00075 
00076         void Reset();
00077 
00078         // Update the hash value
00079         void Update(unsigned char* data, unsigned int len);
00080         bool HashFile(char *szFileName);
00081 
00082         // Finalize hash and report
00083         void Final();
00084         void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
00085         void GetHash(unsigned char *uDest);
00086 
00087 private:
00088         // Private SHA-1 transformation
00089         void Transform(unsigned long state[5], unsigned char buffer[64]);
00090 
00091         // Member variables
00092         unsigned char m_workspace[64];
00093         SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above
00094 };
00095 
00096 #endif // ___SHA1_H___

Generated on Tue Oct 25 23:04:38 2005 for fortress by  doxygen 1.4.2