Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Sha1.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Sha1.h"
14 
15 
16 using namespace kome::core;
17 
18 
19 #include <crtdbg.h>
20 #ifdef _DEBUG
21  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
22  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
23 #endif // _DEBUG
24 
25 
26 
27 // logical function
28 unsigned long Sha1::f( const long t, const unsigned long b, const unsigned long c, const unsigned long d ) {
29  if( t < 20 ) {
30  return ( ( b & c ) | ( ~b & d ) );
31  }
32  if( t < 40 ) {
33  return ( b ^ c ^ d );
34  }
35  if( t < 60 ) {
36  return ( ( b & c ) | ( b & d ) | ( c & d ) );
37  }
38  if( t < 80 ) {
39  return ( b ^ c ^ d );
40  }
41  return 0;
42 }
43 
44 // constant word
45 unsigned long Sha1::K( const long t ) {
46  if( t < 20 ) {
47  return 0x5A827999;
48  }
49  if( t < 40 ) {
50  return 0x6ED9EBA1;
51  }
52  if( t < 60 ) {
53  return 0x8F1BBCDC;
54  }
55  if( t < 80 ) {
56  return 0xCA62C1D6;
57  }
58  return 0;
59 }
60 
61 // transform data
62 std::string Sha1::transformData( void* src, const unsigned int length ) {
63  // cast
64  unsigned char* uc = (unsigned char*)src;
65 
66  // context
67  Sha1Context context;
68  context.appendMessage( uc, length );
69  context.finish();
70 
71  // get string
72  std::string s;
73  for( unsigned int i = 0; i < 5; i++ ) {
74  s.append( FMT( "%08x", context.m_h[ i ] ) );
75  }
76 
77  return s;
78 }
79 
80 // transform file
81 std::string Sha1::transformFile( const char* path ) {
82  // string
83  std::string s;
84 
85  // open the file
86  FILE* fp = fileopen( path, "rb" );
87  if( fp == NULL ) {
88  LOG_ERROR( FMT( "Failed to open the file. [%s]", path ) );
89  return s;
90  }
91 
92  // context
93  Sha1Context context;
94 
95  // read the file
96  unsigned char buff[ 0x1000 ];
97  int readSize = 0;
98  while( ( readSize = fread( buff, 1, 0x1000, fp ) ) > 0 ) {
99  context.appendMessage( buff, readSize );
100  }
101  context.finish();
102 
103  // close file
104  fclose( fp );
105 
106  // get string
107  for( unsigned int i = 0; i < 5; i++ ) {
108  s.append( FMT( "%08x", context.m_h[ i ] ) );
109  }
110 
111  return s;
112 }
113 
114 
115 
116 //
117 // Sha1Context
118 //
119 
120 // constructor
122  reset();
123 }
124 
125 // destructor
127 }
128 
129 // append message
130 void Sha1::Sha1Context::appendMessage( unsigned char* msg, const unsigned long length ) {
131  // check the parameter
132  if( length == 0 ) {
133  return;
134  }
135 
136  // set to the block
137  for( unsigned int i = 0; i < length; i++ ) {
138  m_block[ m_pos++ ] = msg[ i ];
139  // set to block
140  m_length++;
141 
142  // update hash
143  if( m_pos == 64 ) {
144  updateHash();
145  m_pos = 0;
146  fillzero( m_block, sizeof( m_block ) );
147  }
148  }
149 }
150 
151 // finish
153  // padding
154  padBlock();
155 
156  // update hash
157  updateHash();
158 }
159 
160 // update hash
162  // initialize word
163  unsigned long w[ 80 ];
164  for( int t = 0; t < 16; t++ ) {
165  unsigned long wt = 0;
166  for( int i = 0; i < 4; i++ ) {
167  wt = wt << 8;
168  wt |= m_block[ t * 4 + i ];
169  }
170  w[ t ] = wt;
171  }
172  for( int t = 16; t < 80; t++ ) {
173  unsigned long wt = w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ];
174  w[ t ] = CIRCULAR_LEFT_SHIFT( wt, 1 );
175  }
176 
177  // calculate
178  unsigned long a = m_h[ 0 ];
179  unsigned long b = m_h[ 1 ];
180  unsigned long c = m_h[ 2 ];
181  unsigned long d = m_h[ 3 ];
182  unsigned long e = m_h[ 4 ];
183 
184  for( int t = 0; t < 80; t++ ) {
185  unsigned long tmp = CIRCULAR_LEFT_SHIFT( a, 5 ) + Sha1::f( t, b, c, d ) + e + w[ t ] + Sha1::K( t );
186 
187  e = d;
188  d = c;
189  c = CIRCULAR_LEFT_SHIFT( b, 30 );
190  b = a;
191  a = tmp;
192  }
193 
194  // update hash
195  m_h[ 0 ] += a;
196  m_h[ 1 ] += b;
197  m_h[ 2 ] += c;
198  m_h[ 3 ] += d;
199  m_h[ 4 ] += e;
200 }
201 
202 // pads block
204  m_block[ m_pos ] = 0x80;
205  if( m_pos > 55 ) {
206  updateHash();
207  fillzero( m_block, sizeof( m_block ) );
208  }
209 
210  // get bit length
211  unsigned long highLength = m_length >> 29;
212  unsigned long lowLength = m_length << 3;
213 
214  m_block[ 56 ] = (unsigned char)( ( highLength >> 24 ) & 0xff );
215  m_block[ 57 ] = (unsigned char)( ( highLength >> 16 ) & 0xff );
216  m_block[ 58 ] = (unsigned char)( ( highLength >> 8 ) & 0xff );
217  m_block[ 59 ] = (unsigned char)( ( highLength ) & 0xff );
218  m_block[ 60 ] = (unsigned char)( ( lowLength >> 24 ) & 0xff );
219  m_block[ 61 ] = (unsigned char)( ( lowLength >> 16 ) & 0xff );
220  m_block[ 62 ] = (unsigned char)( ( lowLength >> 8 ) & 0xff );
221  m_block[ 63 ] = (unsigned char)( ( lowLength ) & 0xff );
222 }
223 
224 // reset member variables
226  // Hash
227  m_h[ 0 ] = 0x67452301;
228  m_h[ 1 ] = 0xEFCDAB89;
229  m_h[ 2 ] = 0x98BADCFE;
230  m_h[ 3 ] = 0x10325476;
231  m_h[ 4 ] = 0xC3D2E1F0;
232 
233  // block
234  fillzero( m_block, sizeof( m_block ) );
235 
236  // length
237  m_length = 0;
238 
239  // position
240  m_pos = 0;
241 }
static unsigned long f(const long t, const unsigned long b, const unsigned long c, const unsigned long d)
logical function used in SHA1
Definition: Sha1.cpp:28
static std::string transformFile(const char *path)
transform
Definition: Sha1.cpp:81
void finish()
finishes to append message
Definition: Sha1.cpp:152
Sha1Context()
constructor
Definition: Sha1.cpp:121
void appendMessage(unsigned char *msg, const unsigned long length)
appends message
Definition: Sha1.cpp:130
void padBlock()
pads block
Definition: Sha1.cpp:203
unsigned long m_h[5]
Definition: Sha1.h:109
#define CIRCULAR_LEFT_SHIFT(v, bits)
Definition: CoreMacros.h:48
#define NULL
Definition: CoreMacros.h:18
void reset()
resets member variables
Definition: Sha1.cpp:225
FILE * fileopen(const char *path, const char *mode)
opens file
static std::string transformData(void *src, const unsigned int length)
transform data
Definition: Sha1.cpp:62
virtual ~Sha1Context()
destructor
Definition: Sha1.cpp:126
void updateHash()
updates hash
Definition: Sha1.cpp:161
static unsigned long K(const long t)
constant word used in SHA1
Definition: Sha1.cpp:45
interfarces of Sha1 class
SHA1 context.
Definition: Sha1.h:66
void fillzero(void *p, size_t n)
This function fills a block of memory zeros.