Public Member Functions | Private Member Functions | Private Attributes | List of all members
fhicl::SHA1 Class Reference

#include <sha1.h>

Public Member Functions

 SHA1 ()
 
void Reset ()
 
bool Result (unsigned *message_digest_array)
 
void Input (const unsigned char *message_array, unsigned length)
 
void Input (const char *message_array, unsigned length)
 
void Input (unsigned char message_element)
 
void Input (char message_element)
 
SHA1operator<< (const char *message_array)
 
SHA1operator<< (const unsigned char *message_array)
 
SHA1operator<< (const char message_element)
 
SHA1operator<< (const unsigned char message_element)
 

Private Member Functions

void ProcessMessageBlock ()
 
void PadMessage ()
 
unsigned CircularShift (int bits, unsigned word)
 

Private Attributes

unsigned H [5]
 
unsigned Length_Low
 
unsigned Length_High
 
unsigned char Message_Block [64]
 
int Message_Block_Index
 
bool Computed
 
bool Corrupted
 

Detailed Description

Definition at line 29 of file sha1.h.

Constructor & Destructor Documentation

fhicl::SHA1::SHA1 ( )

Definition at line 59 of file sha1.cpp.

59 { Reset(); }
void Reset()
Definition: sha1.cpp:78

Member Function Documentation

unsigned fhicl::SHA1::CircularShift ( int  bits,
unsigned  word 
)
inlineprivate

Definition at line 549 of file sha1.cpp.

550  {
551  return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32 - bits));
552  }
union ptb::content::word::word word
void fhicl::SHA1::Input ( const unsigned char *  message_array,
unsigned  length 
)

Definition at line 152 of file sha1.cpp.

153  {
154  if (!length) {
155  return;
156  }
157 
158  if (Computed || Corrupted) {
159  Corrupted = true;
160  return;
161  }
162 
163  while (length-- && !Corrupted) {
164  Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
165 
166  Length_Low += 8;
167  Length_Low &= 0xFFFFFFFF; // Force it to 32 bits
168  if (Length_Low == 0) {
169  Length_High++;
170  Length_High &= 0xFFFFFFFF; // Force it to 32 bits
171  if (Length_High == 0) {
172  Corrupted = true; // Message is too long
173  }
174  }
175 
176  if (Message_Block_Index == 64) {
178  }
179 
180  message_array++;
181  }
182  }
unsigned Length_Low
Definition: sha1.h:74
unsigned Length_High
Definition: sha1.h:75
int Message_Block_Index
Definition: sha1.h:78
unsigned char Message_Block[64]
Definition: sha1.h:77
bool Computed
Definition: sha1.h:80
bool Corrupted
Definition: sha1.h:81
void ProcessMessageBlock()
Definition: sha1.cpp:386
void fhicl::SHA1::Input ( const char *  message_array,
unsigned  length 
)

Definition at line 205 of file sha1.cpp.

206  {
207  Input((unsigned char*)message_array, length);
208  }
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
void fhicl::SHA1::Input ( unsigned char  message_element)

Definition at line 227 of file sha1.cpp.

228  {
229  Input(&message_element, 1);
230  }
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
void fhicl::SHA1::Input ( char  message_element)

Definition at line 249 of file sha1.cpp.

250  {
251  Input((unsigned char*)&message_element, 1);
252  }
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
SHA1 & fhicl::SHA1::operator<< ( const char *  message_array)

Definition at line 273 of file sha1.cpp.

274  {
275  const char* p = message_array;
276 
277  while (*p) {
278  Input(*p);
279  p++;
280  }
281 
282  return *this;
283  }
p
Definition: test.py:223
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
SHA1 & fhicl::SHA1::operator<< ( const unsigned char *  message_array)

Definition at line 304 of file sha1.cpp.

305  {
306  const unsigned char* p = message_array;
307 
308  while (*p) {
309  Input(*p);
310  p++;
311  }
312 
313  return *this;
314  }
p
Definition: test.py:223
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
SHA1 & fhicl::SHA1::operator<< ( const char  message_element)

Definition at line 334 of file sha1.cpp.

335  {
336  Input((unsigned char*)&message_element, 1);
337 
338  return *this;
339  }
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
SHA1 & fhicl::SHA1::operator<< ( const unsigned char  message_element)

Definition at line 359 of file sha1.cpp.

360  {
361  Input(&message_element, 1);
362 
363  return *this;
364  }
void Input(const unsigned char *message_array, unsigned length)
Definition: sha1.cpp:152
void fhicl::SHA1::PadMessage ( )
private

Definition at line 490 of file sha1.cpp.

491  {
492  /*
493  * Check to see if the current message block is too small to hold
494  * the initial padding bits and length. If so, we will pad the
495  * block, process it, and then continue padding into a second block.
496  */
497  if (Message_Block_Index > 55) {
499  while (Message_Block_Index < 64) {
501  }
502 
504 
505  while (Message_Block_Index < 56) {
507  }
508  } else {
510  while (Message_Block_Index < 56) {
512  }
513  }
514 
515  /*
516  * Store the message length as the last 8 octets
517  */
518  Message_Block[56] = (Length_High >> 24) & 0xFF;
519  Message_Block[57] = (Length_High >> 16) & 0xFF;
520  Message_Block[58] = (Length_High >> 8) & 0xFF;
521  Message_Block[59] = (Length_High)&0xFF;
522  Message_Block[60] = (Length_Low >> 24) & 0xFF;
523  Message_Block[61] = (Length_Low >> 16) & 0xFF;
524  Message_Block[62] = (Length_Low >> 8) & 0xFF;
525  Message_Block[63] = (Length_Low)&0xFF;
526 
528  }
unsigned Length_Low
Definition: sha1.h:74
unsigned Length_High
Definition: sha1.h:75
int Message_Block_Index
Definition: sha1.h:78
unsigned char Message_Block[64]
Definition: sha1.h:77
void ProcessMessageBlock()
Definition: sha1.cpp:386
void fhicl::SHA1::ProcessMessageBlock ( )
private

Definition at line 386 of file sha1.cpp.

387  {
388  const unsigned K[] = {// Constants defined for SHA-1
389  0x5A827999,
390  0x6ED9EBA1,
391  0x8F1BBCDC,
392  0xCA62C1D6};
393  int t; // Loop counter
394  unsigned temp; // Temporary word value
395  unsigned W[80]; // Word sequence
396  unsigned A, B, C, D, E; // Word buffers
397 
398  /*
399  * Initialize the first 16 words in the array W
400  */
401  for (t = 0; t < 16; t++) {
402  W[t] = ((unsigned)Message_Block[t * 4]) << 24;
403  W[t] |= ((unsigned)Message_Block[t * 4 + 1]) << 16;
404  W[t] |= ((unsigned)Message_Block[t * 4 + 2]) << 8;
405  W[t] |= ((unsigned)Message_Block[t * 4 + 3]);
406  }
407 
408  for (t = 16; t < 80; t++) {
409  W[t] = CircularShift(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
410  }
411 
412  A = H[0];
413  B = H[1];
414  C = H[2];
415  D = H[3];
416  E = H[4];
417 
418  for (t = 0; t < 20; t++) {
419  temp = CircularShift(5, A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
420  temp &= 0xFFFFFFFF;
421  E = D;
422  D = C;
423  C = CircularShift(30, B);
424  B = A;
425  A = temp;
426  }
427 
428  for (t = 20; t < 40; t++) {
429  temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[1];
430  temp &= 0xFFFFFFFF;
431  E = D;
432  D = C;
433  C = CircularShift(30, B);
434  B = A;
435  A = temp;
436  }
437 
438  for (t = 40; t < 60; t++) {
439  temp =
440  CircularShift(5, A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
441  temp &= 0xFFFFFFFF;
442  E = D;
443  D = C;
444  C = CircularShift(30, B);
445  B = A;
446  A = temp;
447  }
448 
449  for (t = 60; t < 80; t++) {
450  temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[3];
451  temp &= 0xFFFFFFFF;
452  E = D;
453  D = C;
454  C = CircularShift(30, B);
455  B = A;
456  A = temp;
457  }
458 
459  H[0] = (H[0] + A) & 0xFFFFFFFF;
460  H[1] = (H[1] + B) & 0xFFFFFFFF;
461  H[2] = (H[2] + C) & 0xFFFFFFFF;
462  H[3] = (H[3] + D) & 0xFFFFFFFF;
463  H[4] = (H[4] + E) & 0xFFFFFFFF;
464 
466  }
int Message_Block_Index
Definition: sha1.h:78
unsigned char Message_Block[64]
Definition: sha1.h:77
unsigned H[5]
Definition: sha1.h:72
#define D
Debug message.
Definition: tclscanner.cpp:775
unsigned CircularShift(int bits, unsigned word)
Definition: sha1.cpp:549
E
Definition: 018_def.c:13
#define A
Definition: memgrp.cpp:38
void fhicl::SHA1::Reset ( )

Definition at line 78 of file sha1.cpp.

79  {
80  Length_Low = 0;
81  Length_High = 0;
83 
84  H[0] = 0x67452301;
85  H[1] = 0xEFCDAB89;
86  H[2] = 0x98BADCFE;
87  H[3] = 0x10325476;
88  H[4] = 0xC3D2E1F0;
89 
90  Computed = false;
91  Corrupted = false;
92  }
unsigned Length_Low
Definition: sha1.h:74
unsigned Length_High
Definition: sha1.h:75
int Message_Block_Index
Definition: sha1.h:78
unsigned H[5]
Definition: sha1.h:72
bool Computed
Definition: sha1.h:80
bool Corrupted
Definition: sha1.h:81
bool fhicl::SHA1::Result ( unsigned *  message_digest_array)

Definition at line 113 of file sha1.cpp.

114  {
115  int i; // Counter
116 
117  if (Corrupted) {
118  return false;
119  }
120 
121  if (!Computed) {
122  PadMessage();
123  Computed = true;
124  }
125 
126  for (i = 0; i < 5; i++) {
127  message_digest_array[i] = H[i];
128  }
129 
130  return true;
131  }
unsigned H[5]
Definition: sha1.h:72
void PadMessage()
Definition: sha1.cpp:490
bool Computed
Definition: sha1.h:80
bool Corrupted
Definition: sha1.h:81

Member Data Documentation

bool fhicl::SHA1::Computed
private

Definition at line 80 of file sha1.h.

bool fhicl::SHA1::Corrupted
private

Definition at line 81 of file sha1.h.

unsigned fhicl::SHA1::H[5]
private

Definition at line 72 of file sha1.h.

unsigned fhicl::SHA1::Length_High
private

Definition at line 75 of file sha1.h.

unsigned fhicl::SHA1::Length_Low
private

Definition at line 74 of file sha1.h.

unsigned char fhicl::SHA1::Message_Block[64]
private

Definition at line 77 of file sha1.h.

int fhicl::SHA1::Message_Block_Index
private

Definition at line 78 of file sha1.h.


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