EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PasswordModule.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Captnoord
24 */
25 
26 #include "eve-common.h"
27 
28 #include "auth/PasswordModule.h"
29 #include "auth/ShaModule.h"
30 
32  const std::string& user,
33  const std::string& pass,
34  std::string& hash )
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 }
42 
44  const char* user, size_t userLen,
45  const char* pass, size_t passLen,
46  std::string& hash )
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 }
79 
81  const std::vector< uint16 >& user,
82  const std::vector< uint16 >& pass,
83  std::string& hash )
84 {
85  // Pass it to the next function
86  return GeneratePassHash(
87  &user[0], user.size(),
88  &pass[0], pass.size(),
89  hash );
90 }
91 
93  const uint16* user, size_t userLen,
94  const uint16* pass, size_t passLen,
95  std::string& hash )
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
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.
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