Skip to content

Commit

Permalink
move letter strlen to better pos
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian committed Jul 3, 2023
1 parent 1c40565 commit 0c6dd3f
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions module/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,21 @@ LUA_FUNCTION(MakeSecureRandomNumber)
return 1;
}

const char *B64_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *DEFAULT_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

LUA_FUNCTION(MakeSecureRandomString)
{
size_t len = (size_t)LUA->CheckNumber(1);

bool allowAll = false;
const char *letters = B64_LETTERS;
size_t lettercount = 64;
const char *letters = DEFAULT_LETTERS;
if (LUA->IsType(2, Type::Bool))
{
allowAll = LUA->GetBool(2);
}
else if (LUA->IsType(2, Type::String))
{
letters = LUA->GetString(2);
lettercount = strlen(letters);
}

char *out = (char*)malloc(len + 1);
Expand All @@ -170,6 +168,7 @@ LUA_FUNCTION(MakeSecureRandomString)

if (!allowAll)
{
const size_t lettercount = strlen(letters);
for (size_t i = 0; i < len; i++)
{
out[i] = letters[out[i] % lettercount];
Expand Down

0 comments on commit 0c6dd3f

Please sign in to comment.