CSHA1 Class Reference

#include <SHA1.h>

List of all members.


Detailed Description

Definition at line 97 of file SHA1.h.


Public Types

enum  { REPORT_HEX = 0, REPORT_DIGIT = 1 }

Public Member Functions

 CSHA1 ()
 ~CSHA1 ()
void Reset ()
void Update (UINT_8 *data, UINT_32 len)
bool HashFile (char *szFileName)
void Final ()
void ReportHash (char *szReport, unsigned char uReportType=REPORT_HEX)
void GetHash (UINT_8 *puDest)

Public Attributes

UINT_32 m_state [5]
UINT_32 m_count [2]
UINT_32 __reserved1 [1]
UINT_8 m_buffer [64]
UINT_8 m_digest [20]
UINT_32 __reserved2 [3]

Private Member Functions

void Transform (UINT_32 *state, UINT_8 *buffer)

Private Attributes

UINT_8 m_workspace [64]
SHA1_WORKSPACE_BLOCK * m_block

Member Enumeration Documentation

anonymous enum

Enumerator:
REPORT_HEX 
REPORT_DIGIT 

Definition at line 102 of file SHA1.h.

00103     {
00104         REPORT_HEX = 0,
00105         REPORT_DIGIT = 1
00106     };


Constructor & Destructor Documentation

CSHA1::CSHA1 (  ) 

Definition at line 62 of file SHA1.cc.

00063 {
00064     m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
00065 
00066     Reset();
00067 }

CSHA1::~CSHA1 (  ) 

Definition at line 69 of file SHA1.cc.

00070 {
00071     Reset();
00072 }


Member Function Documentation

void CSHA1::Reset (  ) 

Definition at line 74 of file SHA1.cc.

Referenced by I3Identifier::createFromHash(), CSHA1(), DHT::handleGetRequest(), OverlayKey::sha1(), and ~CSHA1().

00075 {
00076     // SHA1 initialization constants
00077     m_state[0] = 0x67452301;
00078     m_state[1] = 0xEFCDAB89;
00079     m_state[2] = 0x98BADCFE;
00080     m_state[3] = 0x10325476;
00081     m_state[4] = 0xC3D2E1F0;
00082 
00083     m_count[0] = 0;
00084     m_count[1] = 0;
00085 }

void CSHA1::Update ( UINT_8 *  data,
UINT_32  len 
)

Definition at line 191 of file SHA1.cc.

Referenced by I3Identifier::createFromHash(), Final(), DHT::handleGetRequest(), HashFile(), and OverlayKey::sha1().

00192 {
00193     UINT_32 i, j;
00194 
00195     j = (m_count[0] >> 3) & 63;
00196 
00197     if((m_count[0] += len << 3) < (len << 3))
00198         m_count[1]++;
00199 
00200     m_count[1] += (len >> 29);
00201 
00202     if((j + len) > 63) {
00203         i = 64 - j;
00204         memcpy(&m_buffer[j], data, i);
00205         Transform(m_state, m_buffer);
00206 
00207         for( ; i + 63 < len; i += 64)
00208             Transform(m_state, &data[i]);
00209 
00210         j = 0;
00211     } else
00212         i = 0;
00213 
00214     memcpy(&m_buffer[j], &data[i], len - i);
00215 }

bool CSHA1::HashFile ( char *  szFileName  ) 

Definition at line 219 of file SHA1.cc.

00220 {
00221     unsigned long ulFileSize, ulRest, ulBlocks;
00222     unsigned long i;
00223     UINT_8 uData[SHA1_MAX_FILE_BUFFER];
00224     FILE *fIn;
00225 
00226     if(szFileName == NULL)
00227         return false;
00228 
00229     fIn = fopen(szFileName, "rb");
00230     if(fIn == NULL)
00231         return false;
00232 
00233     fseek(fIn, 0, SEEK_END);
00234     ulFileSize = (unsigned long)ftell(fIn);
00235     fseek(fIn, 0, SEEK_SET);
00236 
00237     if(ulFileSize != 0) {
00238         ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER;
00239         ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER;
00240     } else {
00241         ulBlocks = 0;
00242         ulRest = 0;
00243     }
00244 
00245     for(i = 0; i < ulBlocks; i++) {
00246         fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn);
00247         Update((UINT_8 *)uData, SHA1_MAX_FILE_BUFFER);
00248     }
00249 
00250     if(ulRest != 0) {
00251         fread(uData, 1, ulRest, fIn);
00252         Update((UINT_8 *)uData, ulRest);
00253     }
00254 
00255     fclose(fIn);
00256     fIn = NULL;
00257     return true;
00258 }

void CSHA1::Final (  ) 

Definition at line 261 of file SHA1.cc.

Referenced by I3Identifier::createFromHash(), DHT::handleGetRequest(), and OverlayKey::sha1().

00262 {
00263     UINT_32 i;
00264     UINT_8 finalcount[8];
00265 
00266     for(i = 0; i < 8; i++)
00267         finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
00268                                   >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
00269 
00270     Update((UINT_8 *)"\200", 1);
00271 
00272     while ((m_count[0] & 504) != 448)
00273         Update((UINT_8 *)"\0", 1);
00274 
00275     Update(finalcount, 8); // Cause a SHA1Transform()
00276 
00277     for(i = 0; i < 20; i++) {
00278         m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
00279     }
00280 
00281     // Wipe variables for security reasons
00282 #ifdef SHA1_WIPE_VARIABLES
00283     i = 0;
00284     memset(m_buffer, 0, 64);
00285     memset(m_state, 0, 20);
00286     memset(m_count, 0, 8);
00287     memset(finalcount, 0, 8);
00288     Transform(m_state, m_buffer);
00289 #endif
00290 }

void CSHA1::ReportHash ( char *  szReport,
unsigned char  uReportType = REPORT_HEX 
)

Definition at line 294 of file SHA1.cc.

Referenced by DHT::handleGetRequest().

00295 {
00296     unsigned char i;
00297     char szTemp[16];
00298 
00299     if(szReport == NULL)
00300         return;
00301 
00302     if(uReportType == REPORT_HEX) {
00303         //sprintf(szTemp, "%02x", m_digest[0]);
00304         //strcat(szReport, szTemp);
00305 
00306         for(i = 0; i < 20; i++) {
00307             sprintf(szTemp, "%02x", m_digest[i]);
00308             strcat(szReport, szTemp);
00309         }
00310     } else if(uReportType == REPORT_DIGIT) {
00311         sprintf(szTemp, "%u", m_digest[0]);
00312         strcat(szReport, szTemp);
00313 
00314         for(i = 1; i < 20; i++) {
00315             sprintf(szTemp, " %u", m_digest[i]);
00316             strcat(szReport, szTemp);
00317         }
00318     } else
00319         strcpy(szReport, "Error: Unknown report type!");
00320 }

void CSHA1::GetHash ( UINT_8 *  puDest  ) 

Definition at line 324 of file SHA1.cc.

Referenced by I3Identifier::createFromHash(), and OverlayKey::sha1().

00325 {
00326     memcpy(puDest, m_digest, 20);
00327 }

void CSHA1::Transform ( UINT_32 *  state,
UINT_8 *  buffer 
) [private]

Definition at line 87 of file SHA1.cc.

Referenced by Final(), and Update().

00088 {
00089     // Copy state[] to working vars
00090     UINT_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
00091 
00092     memcpy(m_block, buffer, 64);
00093 
00094     // 4 rounds of 20 operations each. Loop unrolled.
00095     _R0(a,b,c,d,e, 0);
00096     _R0(e,a,b,c,d, 1);
00097     _R0(d,e,a,b,c, 2);
00098     _R0(c,d,e,a,b, 3);
00099     _R0(b,c,d,e,a, 4);
00100     _R0(a,b,c,d,e, 5);
00101     _R0(e,a,b,c,d, 6);
00102     _R0(d,e,a,b,c, 7);
00103     _R0(c,d,e,a,b, 8);
00104     _R0(b,c,d,e,a, 9);
00105     _R0(a,b,c,d,e,10);
00106     _R0(e,a,b,c,d,11);
00107     _R0(d,e,a,b,c,12);
00108     _R0(c,d,e,a,b,13);
00109     _R0(b,c,d,e,a,14);
00110     _R0(a,b,c,d,e,15);
00111     _R1(e,a,b,c,d,16);
00112     _R1(d,e,a,b,c,17);
00113     _R1(c,d,e,a,b,18);
00114     _R1(b,c,d,e,a,19);
00115     _R2(a,b,c,d,e,20);
00116     _R2(e,a,b,c,d,21);
00117     _R2(d,e,a,b,c,22);
00118     _R2(c,d,e,a,b,23);
00119     _R2(b,c,d,e,a,24);
00120     _R2(a,b,c,d,e,25);
00121     _R2(e,a,b,c,d,26);
00122     _R2(d,e,a,b,c,27);
00123     _R2(c,d,e,a,b,28);
00124     _R2(b,c,d,e,a,29);
00125     _R2(a,b,c,d,e,30);
00126     _R2(e,a,b,c,d,31);
00127     _R2(d,e,a,b,c,32);
00128     _R2(c,d,e,a,b,33);
00129     _R2(b,c,d,e,a,34);
00130     _R2(a,b,c,d,e,35);
00131     _R2(e,a,b,c,d,36);
00132     _R2(d,e,a,b,c,37);
00133     _R2(c,d,e,a,b,38);
00134     _R2(b,c,d,e,a,39);
00135     _R3(a,b,c,d,e,40);
00136     _R3(e,a,b,c,d,41);
00137     _R3(d,e,a,b,c,42);
00138     _R3(c,d,e,a,b,43);
00139     _R3(b,c,d,e,a,44);
00140     _R3(a,b,c,d,e,45);
00141     _R3(e,a,b,c,d,46);
00142     _R3(d,e,a,b,c,47);
00143     _R3(c,d,e,a,b,48);
00144     _R3(b,c,d,e,a,49);
00145     _R3(a,b,c,d,e,50);
00146     _R3(e,a,b,c,d,51);
00147     _R3(d,e,a,b,c,52);
00148     _R3(c,d,e,a,b,53);
00149     _R3(b,c,d,e,a,54);
00150     _R3(a,b,c,d,e,55);
00151     _R3(e,a,b,c,d,56);
00152     _R3(d,e,a,b,c,57);
00153     _R3(c,d,e,a,b,58);
00154     _R3(b,c,d,e,a,59);
00155     _R4(a,b,c,d,e,60);
00156     _R4(e,a,b,c,d,61);
00157     _R4(d,e,a,b,c,62);
00158     _R4(c,d,e,a,b,63);
00159     _R4(b,c,d,e,a,64);
00160     _R4(a,b,c,d,e,65);
00161     _R4(e,a,b,c,d,66);
00162     _R4(d,e,a,b,c,67);
00163     _R4(c,d,e,a,b,68);
00164     _R4(b,c,d,e,a,69);
00165     _R4(a,b,c,d,e,70);
00166     _R4(e,a,b,c,d,71);
00167     _R4(d,e,a,b,c,72);
00168     _R4(c,d,e,a,b,73);
00169     _R4(b,c,d,e,a,74);
00170     _R4(a,b,c,d,e,75);
00171     _R4(e,a,b,c,d,76);
00172     _R4(d,e,a,b,c,77);
00173     _R4(c,d,e,a,b,78);
00174     _R4(b,c,d,e,a,79);
00175 
00176     // Add the working vars back into state
00177     state[0] += a;
00178     state[1] += b;
00179     state[2] += c;
00180     state[3] += d;
00181     state[4] += e;
00182 
00183     // Wipe variables
00184 #ifdef SHA1_WIPE_VARIABLES
00185 
00186     a = b = c = d = e = 0;
00187 #endif
00188 }


Member Data Documentation

UINT_32 CSHA1::m_state[5]

Definition at line 113 of file SHA1.h.

Referenced by Final(), Reset(), and Update().

UINT_32 CSHA1::m_count[2]

Definition at line 114 of file SHA1.h.

Referenced by Final(), Reset(), and Update().

UINT_32 CSHA1::__reserved1[1]

Definition at line 115 of file SHA1.h.

UINT_8 CSHA1::m_buffer[64]

Definition at line 116 of file SHA1.h.

Referenced by Final(), and Update().

UINT_8 CSHA1::m_digest[20]

Definition at line 117 of file SHA1.h.

Referenced by Final(), GetHash(), and ReportHash().

UINT_32 CSHA1::__reserved2[3]

Definition at line 118 of file SHA1.h.

UINT_8 CSHA1::m_workspace[64] [private]

Definition at line 145 of file SHA1.h.

Referenced by CSHA1().

Definition at line 146 of file SHA1.h.

Referenced by CSHA1(), and Transform().


The documentation for this class was generated from the following files:

Generated on Wed Mar 25 16:32:52 2009 for OverSim by  doxygen 1.5.5