Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

SHA1.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2004-2008 Carnegie Mellon University and University of
00003  * Massachusetts.  All Rights Reserved.
00004  *
00005  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00006  * is subject to the terms of the software license set forth in the LICENSE
00007  * file included with this software, and also available at
00008  * http://www.lemurproject.org/license.html
00009  *
00010  *==========================================================================
00011 */
00012 
00013 #ifndef _SHA1_HPP
00014 #define _SHA1_HPP
00015 
00016 namespace lemur {
00017   namespace utility {
00018 
00024   /*
00025    *  32-bit integer manipulation macros (big endian)
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     }; // end class SHA1
00081 
00082   } // end namespace utility
00083 } // end namespace lemur
00084 
00085 #endif // _SHA1_HPP

Generated on Tue Jun 15 11:02:55 2010 for Lemur by doxygen 1.3.4