EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ShaModule Class Reference

Sha1 module for username + password hashes. More...

#include "ShaModule.h"

Classes

struct  SHAobject
 

Public Types

typedef uint8 SHA_BYTE
 
typedef uint32 SHA_INT32
 

Static Public Member Functions

static void longReverse (SHA_INT32 *buffer, int byteCount, int Endianness)
 
static void SHAcopy (SHAobject *src, SHAobject *dest)
 
static void sha_transform (SHAobject *sha_info)
 
static void sha_init (SHAobject *sha_info)
 
static void sha_update (SHAobject *sha_info, const SHA_BYTE *buffer, int count)
 
static void sha_update (SHAobject *sha_info, const std::wstring &value)
 
static void sha_final (unsigned char digest[20], SHAobject *sha_info)
 
static void sha_digest (SHAobject *self, uint8 *digest)
 
static void sha_digest (SHAobject *self, std::string &digest)
 
static std::string Hexify (const char *data, size_t len)
 
static std::string SHA_hexdigest (SHAobject *self)
 

Detailed Description

Sha1 module for username + password hashes.

Author
the original SHA guys, python guys, Captnoord
Date
January 2009
Todo:
move code to cpp file, clean and comment.

Definition at line 38 of file ShaModule.h.

Member Typedef Documentation

Definition at line 60 of file ShaModule.h.

Definition at line 61 of file ShaModule.h.

Member Function Documentation

static std::string ShaModule::Hexify ( const char *  data,
size_t  len 
)
inlinestatic

Hexify is a small until function to do a simple convention to hex representation.

Parameters
[in]datathe char array that contains the data
[in]lenthe length of data
Returns
in input data converted to a readable hex representation.

Definition at line 391 of file ShaModule.h.

392  {
393  char *hex_digest;
394  size_t i, j;
395  std::string retval;
396  retval.resize(len * 2);
397  hex_digest = &retval[0];
398 
399  for(i=j=0; i<len; i++) {
400  char c;
401  c = (data[i] >> 4) & 0xf;
402  c = (c>9) ? c+'a'-10 : c + '0';
403  hex_digest[j++] = c;
404  c = (data[i] & 0xf);
405  c = (c>9) ? c+'a'-10 : c + '0';
406  hex_digest[j++] = c;
407  }
408  return retval;
409  }
static void ShaModule::longReverse ( SHA_INT32 buffer,
int  byteCount,
int  Endianness 
)
inlinestatic

Definition at line 81 of file ShaModule.h.

References PCT_BIG_ENDIAN.

Referenced by sha_transform().

82  {
83  SHA_INT32 value;
84 
85  if ( Endianness == PCT_BIG_ENDIAN )
86  return;
87 
88  byteCount /= sizeof(*buffer);
89  while (byteCount--) {
90  value = *buffer;
91  value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
92  ( ( value & 0x00FF00FFL ) << 8 );
93  *buffer++ = ( value << 16 ) | ( value >> 16 );
94  }
95  }
uint32 SHA_INT32
Definition: ShaModule.h:61
#define PCT_BIG_ENDIAN
Definition: ShaModule.h:57

Here is the caller graph for this function:

static void ShaModule::sha_digest ( SHAobject self,
uint8 digest 
)
inlinestatic

Definition at line 363 of file ShaModule.h.

References sha_final(), and SHAcopy().

Referenced by PasswordModule::GeneratePassHash().

364  {
365  if (digest == NULL)
366  return;
367  SHAobject temp;
368  SHAcopy(self, &temp);
369  sha_final(digest, &temp);
370  }
static void sha_final(unsigned char digest[20], SHAobject *sha_info)
Definition: ShaModule.h:309
static void SHAcopy(SHAobject *src, SHAobject *dest)
Definition: ShaModule.h:97

Here is the call graph for this function:

Here is the caller graph for this function:

static void ShaModule::sha_digest ( SHAobject self,
std::string &  digest 
)
inlinestatic

Definition at line 373 of file ShaModule.h.

References SHA_DIGESTSIZE, sha_final(), and SHAcopy().

374  {
375  if (digest.size() < SHA_DIGESTSIZE)
376  digest.resize(SHA_DIGESTSIZE);
377 
378  SHAobject temp;
379  SHAcopy(self, &temp);
380  sha_final((unsigned char*)&digest[0], &temp);
381  }
static void sha_final(unsigned char digest[20], SHAobject *sha_info)
Definition: ShaModule.h:309
#define SHA_DIGESTSIZE
Definition: ShaModule.h:66
static void SHAcopy(SHAobject *src, SHAobject *dest)
Definition: ShaModule.h:97

Here is the call graph for this function:

static void ShaModule::sha_final ( unsigned char  digest[20],
SHAobject sha_info 
)
inlinestatic

Definition at line 309 of file ShaModule.h.

References ShaModule::SHAobject::count_hi, ShaModule::SHAobject::count_lo, ShaModule::SHAobject::data, ShaModule::SHAobject::digest, SHA_BLOCKSIZE, and sha_transform().

Referenced by PasswordModule::GeneratePassHash(), sha_digest(), and SHA_hexdigest().

310  {
311  int count;
312  SHA_INT32 lo_bit_count, hi_bit_count;
313 
314  lo_bit_count = sha_info->count_lo;
315  hi_bit_count = sha_info->count_hi;
316  count = (int) ((lo_bit_count >> 3) & 0x3f);
317  ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
318  if (count > SHA_BLOCKSIZE - 8) {
319  memset(((SHA_BYTE *) sha_info->data) + count, 0,
320  SHA_BLOCKSIZE - count);
321  sha_transform(sha_info);
322  memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
323  }
324  else {
325  memset(((SHA_BYTE *) sha_info->data) + count, 0,
326  SHA_BLOCKSIZE - 8 - count);
327  }
328 
329  /* GJS: note that we add the hi/lo in big-endian. sha_transform will
330  swap these values into host-order. */
331  sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
332  sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
333  sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
334  sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
335  sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
336  sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
337  sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
338  sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
339  sha_transform(sha_info);
340  digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
341  digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
342  digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
343  digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
344  digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
345  digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
346  digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
347  digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
348  digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
349  digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
350  digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
351  digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
352  digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
353  digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
354  digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
355  digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
356  digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
357  digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
358  digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
359  digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
360  }
uint32 SHA_INT32
Definition: ShaModule.h:61
uint8 SHA_BYTE
Definition: ShaModule.h:60
#define SHA_BLOCKSIZE
Definition: ShaModule.h:65
static void sha_transform(SHAobject *sha_info)
Definition: ShaModule.h:184

Here is the call graph for this function:

Here is the caller graph for this function:

static std::string ShaModule::SHA_hexdigest ( SHAobject self)
inlinestatic

Definition at line 412 of file ShaModule.h.

References SHA_DIGESTSIZE, sha_final(), and SHAcopy().

413  {
414  unsigned char digest[SHA_DIGESTSIZE];
415  SHAobject temp;
416  std::string retval;
417  char *hex_digest;
418  int i, j;
419 
420  /* Get the raw (binary) digest value */
421  SHAcopy(self, &temp);
422  sha_final(digest, &temp);
423 
424  /* Create a new string */
425  //retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
426  retval.resize(sizeof(digest) * 2);
427  hex_digest = &retval[0];
428 
429  /* Make hex version of the digest */
430  for(i=j=0; i<(int)sizeof(digest); i++) {
431  char c;
432  c = (digest[i] >> 4) & 0xf;
433  c = (c>9) ? c+'a'-10 : c + '0';
434  hex_digest[j++] = c;
435  c = (digest[i] & 0xf);
436  c = (c>9) ? c+'a'-10 : c + '0';
437  hex_digest[j++] = c;
438  }
439  return retval;
440  }
static void sha_final(unsigned char digest[20], SHAobject *sha_info)
Definition: ShaModule.h:309
#define SHA_DIGESTSIZE
Definition: ShaModule.h:66
static void SHAcopy(SHAobject *src, SHAobject *dest)
Definition: ShaModule.h:97

Here is the call graph for this function:

static void ShaModule::sha_init ( SHAobject sha_info)
inlinestatic

Definition at line 245 of file ShaModule.h.

References ShaModule::SHAobject::count_hi, ShaModule::SHAobject::count_lo, ShaModule::SHAobject::digest, ShaModule::SHAobject::Endianness, ShaModule::SHAobject::local, and TestEndianness.

Referenced by PasswordModule::GeneratePassHash().

246  {
247  TestEndianness(sha_info->Endianness)
248 
249  sha_info->digest[0] = 0x67452301L;
250  sha_info->digest[1] = 0xefcdab89L;
251  sha_info->digest[2] = 0x98badcfeL;
252  sha_info->digest[3] = 0x10325476L;
253  sha_info->digest[4] = 0xc3d2e1f0L;
254  sha_info->count_lo = 0L;
255  sha_info->count_hi = 0L;
256  sha_info->local = 0;
257  }
#define TestEndianness(variable)
Definition: ShaModule.h:53

Here is the caller graph for this function:

static void ShaModule::sha_transform ( SHAobject sha_info)
inlinestatic

Definition at line 184 of file ShaModule.h.

References ShaModule::SHAobject::data, ShaModule::SHAobject::digest, EvE::Trig::E, ShaModule::SHAobject::Endianness, FA, FB, FC, FD, FE, FG, FT, longReverse(), and R32.

Referenced by sha_final(), and sha_update().

185  {
186  int i;
187  SHA_INT32 T, A, B, C, D, E, W[80], *WP;
188 
189  memcpy(W, sha_info->data, sizeof(sha_info->data));
190  longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
191 
192  for (i = 16; i < 80; ++i) {
193  W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
194 
195  /* extra rotation fix */
196  W[i] = R32(W[i], 1);
197  }
198  A = sha_info->digest[0];
199  B = sha_info->digest[1];
200  C = sha_info->digest[2];
201  D = sha_info->digest[3];
202  E = sha_info->digest[4];
203  WP = W;
204 #ifdef UNRAVEL
205  FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
206  FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
207  FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
208  FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
209  FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
210  FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
211  FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
212  FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
213  sha_info->digest[0] += E;
214  sha_info->digest[1] += T;
215  sha_info->digest[2] += A;
216  sha_info->digest[3] += B;
217  sha_info->digest[4] += C;
218 #else /* !UNRAVEL */
219 #ifdef UNROLL_LOOPS
220  FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
221  FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
222  FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
223  FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
224  FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
225  FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
226  FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
227  FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
228 #else /* !UNROLL_LOOPS */
229  for (i = 0; i < 20; ++i) { FG(1); }
230  for (i = 20; i < 40; ++i) { FG(2); }
231  for (i = 40; i < 60; ++i) { FG(3); }
232  for (i = 60; i < 80; ++i) { FG(4); }
233 #endif /* !UNROLL_LOOPS */
234  sha_info->digest[0] += A;
235  sha_info->digest[1] += B;
236  sha_info->digest[2] += C;
237  sha_info->digest[3] += D;
238  sha_info->digest[4] += E;
239 #endif /* !UNRAVEL */
240  }
#define FG(n)
Definition: ShaModule.h:157
uint32 SHA_INT32
Definition: ShaModule.h:61
#define FA(n)
Definition: ShaModule.h:163
#define FB(n)
Definition: ShaModule.h:166
#define FC(n)
Definition: ShaModule.h:169
#define FE(n)
Definition: ShaModule.h:175
#define FD(n)
Definition: ShaModule.h:172
const double E
Definition: Trig.h:19
#define FT(n)
Definition: ShaModule.h:178
static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
Definition: ShaModule.h:81
#define R32(x, n)
Definition: ShaModule.h:153

Here is the call graph for this function:

Here is the caller graph for this function:

static void ShaModule::sha_update ( SHAobject sha_info,
const SHA_BYTE buffer,
int  count 
)
inlinestatic

Definition at line 261 of file ShaModule.h.

References ShaModule::SHAobject::count_hi, ShaModule::SHAobject::count_lo, ShaModule::SHAobject::data, ShaModule::SHAobject::local, SHA_BLOCKSIZE, and sha_transform().

Referenced by PasswordModule::GeneratePassHash(), and sha_update().

262  {
263  int i;
264  SHA_INT32 clo;
265 
266  clo = sha_info->count_lo + ((SHA_INT32) count << 3);
267  if (clo < sha_info->count_lo) {
268  ++sha_info->count_hi;
269  }
270  sha_info->count_lo = clo;
271  sha_info->count_hi += (SHA_INT32) count >> 29;
272  if (sha_info->local) {
273  i = SHA_BLOCKSIZE - sha_info->local;
274  if (i > count) {
275  i = count;
276  }
277  memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
278  count -= i;
279  buffer += i;
280  sha_info->local += i;
281  if (sha_info->local == SHA_BLOCKSIZE) {
282  sha_transform(sha_info);
283  }
284  else {
285  return;
286  }
287  }
288  while (count >= SHA_BLOCKSIZE) {
289  memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
290  buffer += SHA_BLOCKSIZE;
291  count -= SHA_BLOCKSIZE;
292  sha_transform(sha_info);
293  }
294  memcpy(sha_info->data, buffer, count);
295  sha_info->local = count;
296  }
uint32 SHA_INT32
Definition: ShaModule.h:61
uint8 SHA_BYTE
Definition: ShaModule.h:60
#define SHA_BLOCKSIZE
Definition: ShaModule.h:65
static void sha_transform(SHAobject *sha_info)
Definition: ShaModule.h:184

Here is the call graph for this function:

Here is the caller graph for this function:

static void ShaModule::sha_update ( SHAobject sha_info,
const std::wstring &  value 
)
inlinestatic

Definition at line 299 of file ShaModule.h.

References sha_update().

300  {
301  sha_update( sha_info,
302  reinterpret_cast< const SHA_BYTE* >( value.c_str() ),
303  value.size() * sizeof( wchar_t ) );
304  }
static void sha_update(SHAobject *sha_info, const SHA_BYTE *buffer, int count)
Definition: ShaModule.h:261

Here is the call graph for this function:

static void ShaModule::SHAcopy ( SHAobject src,
SHAobject dest 
)
inlinestatic

Definition at line 97 of file ShaModule.h.

References ShaModule::SHAobject::count_hi, ShaModule::SHAobject::count_lo, ShaModule::SHAobject::data, ShaModule::SHAobject::digest, ShaModule::SHAobject::Endianness, and ShaModule::SHAobject::local.

Referenced by sha_digest(), and SHA_hexdigest().

98  {
99  dest->Endianness = src->Endianness;
100  dest->local = src->local;
101  dest->count_lo = src->count_lo;
102  dest->count_hi = src->count_hi;
103  memcpy(dest->digest, src->digest, sizeof(src->digest));
104  memcpy(dest->data, src->data, sizeof(src->data));
105  }

Here is the caller graph for this function:


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