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

simple class to handle password hash generation More...

#include "PasswordModule.h"

Static Public Member Functions

static bool GeneratePassHash (const std::string &user, const std::string &pass, std::string &hash)
 Generates a SHA-1 hash from a username and a password. More...
 
static bool GeneratePassHash (const char *user, size_t userLen, const char *pass, size_t passLen, std::string &hash)
 Generates a SHA-1 hash from a username and a password. More...
 
static bool GeneratePassHash (const std::vector< uint16 > &user, const std::vector< uint16 > &pass, std::string &hash)
 Generates a SHA-1 hash from a username and a password. More...
 
static bool GeneratePassHash (const uint16 *user, size_t userLen, const uint16 *pass, size_t passLen, std::string &hash)
 Generates a SHA-1 hash from a username and a password. More...
 

Detailed Description

simple class to handle password hash generation

this class mainly acts like a shell for the password hash stuff.

Author
Captnoord
Date
January 2009

Definition at line 39 of file PasswordModule.h.

Member Function Documentation

bool PasswordModule::GeneratePassHash ( const std::string &  user,
const std::string &  pass,
std::string &  hash 
)
static

Generates a SHA-1 hash from a username and a password.

Parameters
[in]userThe username.
[in]passThe password.
[out]hashWhere to place the resultant SHA-1 hash.
Note
This is a relatively slow function, so I recommend not to use it to calculate hashes on the fly.

Definition at line 31 of file PasswordModule.cpp.

Referenced by APIServiceManager::_AuthenticateUserNamePassword(), auth_PasswordModuleTest(), and GeneratePassHash().

35 {
36  // Pass it to the next function
37  return GeneratePassHash(
38  user.c_str(), user.length(),
39  pass.c_str(), pass.length(),
40  hash );
41 }
static bool GeneratePassHash(const std::string &user, const std::string &pass, std::string &hash)
Generates a SHA-1 hash from a username and a password.

Here is the caller graph for this function:

bool PasswordModule::GeneratePassHash ( const char *  user,
size_t  userLen,
const char *  pass,
size_t  passLen,
std::string &  hash 
)
static

Generates a SHA-1 hash from a username and a password.

Parameters
[in]userThe username.
[in]userLenLength of the username.
[in]passThe password.
[in]passLenLength of the password.
[out]hashWhere to place the resultant SHA-1 hash.
Note
This is a relatively slow function, so I recommend not to use it to calculate hashes on the fly.

Definition at line 43 of file PasswordModule.cpp.

References GeneratePassHash(), and utf8to16().

47 {
48  // Convert username and password to UTF-16
49  std::vector< uint16 > username, password;
50  utf8::utf8to16( user, user + userLen,
51  std::back_inserter( username ) );
52  utf8::utf8to16( pass, pass + passLen,
53  std::back_inserter( password ) );
54 
55  // Lowercase the username
56  std::transform( username.begin(), username.end(),
57  username.begin(), ::tolower );
58 
59  // Find index of first non-space
60  const size_t frontIndex =
61  std::find_if( username.begin(), username.end(),
62  std::not1( std::ptr_fun( ::isspace ) ) )
63  - username.begin();
64  // Find reverse index of last non-space
65  const size_t backIndex =
66  std::find_if( username.rbegin(), username.rend(),
67  std::not1( std::ptr_fun( ::isspace ) ) )
68  - username.rbegin();
69 
70  // Trim the username
71  username.erase( username.begin(),
72  username.begin() + frontIndex );
73  username.erase( username.end() - backIndex,
74  username.end() );
75 
76  // Pass it to the next function
77  return GeneratePassHash( username, password, hash );
78 }
std::u16string utf8to16(std::string &str)
Definition: utfUtils.cpp:289
static bool GeneratePassHash(const std::string &user, const std::string &pass, std::string &hash)
Generates a SHA-1 hash from a username and a password.

Here is the call graph for this function:

bool PasswordModule::GeneratePassHash ( const std::vector< uint16 > &  user,
const std::vector< uint16 > &  pass,
std::string &  hash 
)
static

Generates a SHA-1 hash from a username and a password.

The input username and password must be encoded in UTF-16 and the username must be lowercased and trimmed to give expected results.

Parameters
[in]userThe username.
[in]passThe password.
[out]hashWhere to place the resultant SHA-1 hash.
Note
This is a relatively slow function, so I recommend not to use it to calculate hashes on the fly.

Definition at line 80 of file PasswordModule.cpp.

References GeneratePassHash().

84 {
85  // Pass it to the next function
86  return GeneratePassHash(
87  &user[0], user.size(),
88  &pass[0], pass.size(),
89  hash );
90 }
static bool GeneratePassHash(const std::string &user, const std::string &pass, std::string &hash)
Generates a SHA-1 hash from a username and a password.

Here is the call graph for this function:

bool PasswordModule::GeneratePassHash ( const uint16 user,
size_t  userLen,
const uint16 pass,
size_t  passLen,
std::string &  hash 
)
static

Generates a SHA-1 hash from a username and a password.

The input username and password must be encoded in UTF-16 and the username must be lowercased and trimmed to give expected results.

Parameters
[in]userThe username.
[in]userLenLength of the username (in UTF-16 characters).
[in]passThe password.
[in]passLenLength of the password (in UTF-16 characters).
[out]hashWhere to place the resultant SHA-1 hash.
Note
This is a relatively slow function, so I recommend not to use it to calculate hashes on the fly.

Definition at line 92 of file PasswordModule.cpp.

References ShaModule::sha_digest(), SHA_DIGESTSIZE, ShaModule::sha_final(), ShaModule::sha_init(), and ShaModule::sha_update().

96 {
97  // Define byte pointers (due to readability)
98  const uint8* _user = reinterpret_cast< const uint8* >( user );
99  size_t _userLen = userLen * sizeof( uint16 );
100  const uint8* _pass = reinterpret_cast< const uint8* >( pass );
101  size_t _passLen = passLen * sizeof( uint16 );
102 
103  // Helper digest buffer
104  uint8 digest[ SHA_DIGESTSIZE ];
105 
106  // Initialize the hash
107  ShaModule::SHAobject shaObj;
108  ShaModule::sha_init( &shaObj );
109  ShaModule::sha_update( &shaObj, _pass, _passLen );
110  ShaModule::sha_update( &shaObj, _user, _userLen );
111 
112  // The hashing loop
113  for( size_t i = 0; i < 1000; ++i )
114  {
115  // Store the digest
116  ShaModule::sha_digest( &shaObj, digest );
117 
118  // Rehash the whole stuff
119  ShaModule::sha_init( &shaObj );
120  ShaModule::sha_update( &shaObj, digest, SHA_DIGESTSIZE );
121  ShaModule::sha_update( &shaObj, _user, _userLen );
122  }
123 
124  // Obtain the resulting hash
125  ShaModule::sha_final( digest, &shaObj );
126  hash.assign(
127  reinterpret_cast< char* >( digest ),
128  SHA_DIGESTSIZE );
129 
130  return true;
131 }
unsigned __int8 uint8
Definition: eve-compat.h:46
static void sha_final(unsigned char digest[20], SHAobject *sha_info)
Definition: ShaModule.h:309
#define SHA_DIGESTSIZE
Definition: ShaModule.h:66
static void sha_digest(SHAobject *self, uint8 *digest)
Definition: ShaModule.h:363
unsigned __int16 uint16
Definition: eve-compat.h:48
static void sha_update(SHAobject *sha_info, const SHA_BYTE *buffer, int count)
Definition: ShaModule.h:261
static void sha_init(SHAobject *sha_info)
Definition: ShaModule.h:245

Here is the call graph for this function:


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