00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _SHA1_HPP
00014 #define _SHA1_HPP
00015
00016 namespace lemur {
00017 namespace utility {
00018
00024
00025
00026
00027 #ifndef GET_ULONG_BE
00028 #define GET_ULONG_BE(n,b,i) \
00029 { \
00030 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
00031 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
00032 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
00033 | ( (unsigned long) (b)[(i) + 3] ); \
00034 }
00035 #endif
00036
00037 #ifndef PUT_ULONG_BE
00038 #define PUT_ULONG_BE(n,b,i) \
00039 { \
00040 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00041 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00042 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00043 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00044 }
00045 #endif
00046
00047 static const unsigned char sha1_padding[64] = {
00048 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00049 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00050 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00051 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00052 };
00053
00054 class SHA1 {
00055 private:
00056
00057 typedef struct {
00058 unsigned long total[2];
00059 unsigned long state[5];
00060 unsigned long buffer[64];
00061 unsigned long innerPad[64];
00062 unsigned long outerPad[64];
00063 } SHA1Context;
00064
00065 void start(SHA1Context *context);
00066 void process(SHA1Context *context, unsigned char data[64]);
00067 void update(SHA1Context *context, unsigned char *input, int inputLen);
00068 void finish(SHA1Context *context, unsigned char *output);
00069
00070 char intToHexDigit(char val);
00071 void byteToHexString(unsigned char *input, int inputLen, char *output, int maxOutputLen);
00072
00073 public:
00074 SHA1();
00075 ~SHA1();
00076
00077 void hash(unsigned char *input, int inputLen, unsigned char *output);
00078 void hashStringToHex(const char *input, char *output, int maxOutputLen);
00079
00080 };
00081
00082 }
00083 }
00084
00085 #endif // _SHA1_HPP