Skip to content

Commit a542e55

Browse files
author
Necktrox
committed
Added bcrypt module
1 parent 7c86056 commit a542e55

31 files changed

+3929
-2
lines changed

modules/bcrypt/CFunctions.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*********************************************************
2+
*
3+
* Multi Theft Auto: San Andreas - Deathmatch
4+
*
5+
* ml_base, External lua add-on module
6+
*
7+
* Copyright © 2003-2008 MTA. All Rights Reserved.
8+
*
9+
* Grand Theft Auto is © 2002-2003 Rockstar North
10+
*
11+
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
12+
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
13+
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
14+
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
15+
* PROVIDED WITH THIS PACKAGE.
16+
*
17+
*********************************************************/
18+
19+
#include "CFunctions.h"
20+
#include "extra/CLuaArguments.h"
21+
#include <cstring>
22+
#include <random>
23+
#include <algorithm>
24+
25+
namespace blowfish
26+
{
27+
extern "C"
28+
{
29+
#include "libs/blowfish/ow-crypt.h"
30+
}
31+
}
32+
33+
#define HASH_SIZE 60
34+
#define SALT_SIZE 30
35+
#define ENTROPY_SIZE 32
36+
37+
38+
int CFunctions::BcryptDigest ( lua_State* L )
39+
{
40+
if ( L )
41+
{
42+
const char* key = luaL_checkstring ( L, 1 );
43+
const char* salt = luaL_checkstring ( L, 2 );
44+
45+
char hash [HASH_SIZE+1];
46+
blowfish::crypt_rn ( key, salt, hash, sizeof(hash) );
47+
lua_pushlstring ( L, hash, HASH_SIZE );
48+
49+
return 1;
50+
}
51+
52+
lua_pushboolean ( L, false );
53+
return 1;
54+
}
55+
56+
int CFunctions::BcryptSalt ( lua_State* L )
57+
{
58+
if ( L )
59+
{
60+
unsigned long logRounds = luaL_checkinteger ( L, 1 );
61+
62+
char salt [SALT_SIZE];
63+
char entropy [ENTROPY_SIZE];
64+
65+
std::random_device rd;
66+
std::mt19937 gen ( rd ( ) );
67+
std::generate_n ( entropy, ENTROPY_SIZE, gen );
68+
69+
70+
blowfish::crypt_gensalt_rn ( "$2y$", logRounds, entropy, sizeof ( entropy ), salt, sizeof ( salt ) );
71+
lua_pushlstring ( L, salt, sizeof ( salt ) );
72+
73+
return 1;
74+
}
75+
76+
lua_pushboolean ( L, false );
77+
return 1;
78+
}
79+
80+
int CFunctions::BcryptVerify ( lua_State* L )
81+
{
82+
if ( L )
83+
{
84+
const char* key = luaL_checkstring ( L, 1 );
85+
const char* digest = luaL_checkstring ( L, 2 );
86+
87+
char hash [HASH_SIZE+1];
88+
memset ( hash, 0, sizeof ( hash ) );
89+
90+
blowfish::crypt_rn ( key, digest, hash, sizeof ( hash ) );
91+
92+
int verified = strncmp ( hash, digest, sizeof ( hash ) ) == 0;
93+
94+
lua_pushboolean ( L, verified );
95+
96+
return 1;
97+
}
98+
99+
lua_pushboolean ( L, false );
100+
return 1;
101+
}

modules/bcrypt/CFunctions.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*********************************************************
2+
*
3+
* Multi Theft Auto: San Andreas - Deathmatch
4+
*
5+
* ml_base, External lua add-on module
6+
*
7+
* Copyright © 2003-2008 MTA. All Rights Reserved.
8+
*
9+
* Grand Theft Auto is © 2002-2003 Rockstar North
10+
*
11+
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
12+
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
13+
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
14+
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
15+
* PROVIDED WITH THIS PACKAGE.
16+
*
17+
*********************************************************/
18+
19+
class CFunctions;
20+
21+
#ifndef __CFUNCTIONS_H
22+
#define __CFUNCTIONS_H
23+
24+
#include <stdio.h>
25+
26+
#include "include/ILuaModuleManager.h"
27+
extern ILuaModuleManager10 *pModuleManager;
28+
29+
class CFunctions
30+
{
31+
public:
32+
33+
static int BcryptDigest ( lua_State* luaVM );
34+
static int BcryptSalt ( lua_State* luaVM );
35+
static int BcryptVerify ( lua_State* luaVM );
36+
37+
};
38+
#endif

modules/bcrypt/Common.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*********************************************************
2+
*
3+
* Multi Theft Auto: San Andreas - Deathmatch
4+
*
5+
* ml_base, External lua add-on module
6+
*
7+
* Copyright � 2003-2008 MTA. All Rights Reserved.
8+
*
9+
* Grand Theft Auto is � 2002-2003 Rockstar North
10+
*
11+
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
12+
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
13+
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
14+
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
15+
* PROVIDED WITH THIS PACKAGE.
16+
*
17+
*********************************************************/
18+
19+
extern "C"
20+
{
21+
#include "lua.h"
22+
#include "lualib.h"
23+
#include "lauxlib.h"
24+
}
25+
26+
#ifdef WIN32
27+
#define MTAEXPORT extern "C" __declspec(dllexport)
28+
#else
29+
#define MTAEXPORT extern "C"
30+
#endif
31+
32+
using namespace std;
33+
34+
#ifndef __COMMON_H
35+
#define __COMMON_H
36+
37+
// used in the function argument vector
38+
#define MAX_ARGUMENTS 10
39+
struct FunctionArguments
40+
{
41+
lua_State* luaVM;
42+
unsigned char nArguments;
43+
unsigned char Type[10];
44+
void* Arguments[10];
45+
};
46+
47+
namespace FunctionArgumentType
48+
{
49+
enum
50+
{
51+
TYPE_NUMBER = 1,
52+
TYPE_STRING = 2,
53+
TYPE_LIGHTUSERDATA = 3,
54+
TYPE_BOOLEAN = 4,
55+
TYPE_NIL = 5,
56+
TYPE_TABLE = 6
57+
};
58+
}
59+
#endif

modules/bcrypt/LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2008 Multi Theft Auto
2+
3+
This software is provided 'as-is', without any express or implied
4+
warranty. In no event will the authors be held liable for any damages
5+
arising from the use of this software.
6+
7+
Permission is granted to anyone to use this software for any purpose,
8+
excluding commercial applications, and to alter it and redistribute it
9+
freely, subject to the following restrictions:
10+
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
16+
2. Altered source versions must be plainly marked as such, and must not be
17+
misrepresented as being the original software.
18+
19+
3. This notice may not be removed or altered from any source
20+
distribution.

modules/bcrypt/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ml_bcrypt - MTA bcrypt module
2+
3+
Bcrypt module for MTA:SA, for your passwords. Just three handy functions: `bcrypt_digest`, `bcrypt_salt`, and `bcrypt_verify`.
4+
5+
## Compiling
6+
### Windows
7+
```
8+
premake5.exe vs2015
9+
```
10+
The project files are available in `Build/` then.
11+
12+
### Linux
13+
```
14+
./premake5 gmake
15+
16+
# Use either of the following commands
17+
make all # Builds all (both debug and release for x86 and x64 - you'll need gcc-multilib then, not recommended - use one of the commands below instead)
18+
make config=release_x86 all # Release build for the x86 platform
19+
make config=release_x64 all # Release build for the x86_64 platform
20+
```
21+
22+
## Documentation
23+
### bcrypt_digest
24+
string bcrypt_digest(string key, string salt)
25+
Returns the hash.
26+
27+
### bcrypt_salt
28+
string bcrypt_salt(int logRounds)
29+
Please visit [this link](http://security.stackexchange.com/questions/17207/recommended-of-rounds-for-bcrypt) to determine the number of rounds appropriate for your server.
30+
Returns the salt.
31+
32+
### bcrypt_verify
33+
bool bcrypt_verify(string key, string digest)
34+
Returns whether it is verified. [How does it get the salt?](http://stackoverflow.com/a/6833165/1517394)
35+
36+
### Example
37+
Here's some code that explains the use of all these functions, remember that the database functions mentioned in this aren't real functions and are just for this demonstration.
38+
```lua
39+
-- Get this information by conventional means
40+
myName = "qaisjp"
41+
myRegisterPassword = "LoLIcon"
42+
43+
-- When registering
44+
-- A higher amount of rounds might result in your server freezing for several seconds/minutes
45+
-- Dev notes: A rewrite of the resource should use a separate thread for the log rounds
46+
mySalt = bcrypt_salt(15)
47+
hashedPassword = bcrypt_digest(myRegisterPassword, mySalt)
48+
savePasswordInDatabase(myName, hashedPassword)
49+
50+
-- Now I want to login
51+
myLoginPassword = "LoLIcon"
52+
if bcrypt_verify(hashedPasswordFromDatabase, myLoginPassword) then
53+
outputChatBox("Password verified")
54+
end
55+
```

0 commit comments

Comments
 (0)