Small improvement to the password generator.
This commit is contained in:
parent
c63c3bffe6
commit
b39b7cafaa
@ -158,7 +158,7 @@ int MAIN(const int argc, CHR *const argv[])
|
||||
/* Parse arguments */
|
||||
/* ----------------------------------------------------- */
|
||||
|
||||
if (argc < 1)
|
||||
if ((argc < 1) || (!argv[0]))
|
||||
{
|
||||
FPUTS(T("Error: Argument array is empty. The program was called incorrectly!\n\n"), stderr);
|
||||
goto clean_up;
|
||||
|
@ -21,6 +21,8 @@
|
||||
// Constants
|
||||
// ==========================================================================
|
||||
|
||||
static const size_t MIX_ROUNDS = 97U;
|
||||
|
||||
static const char PASSWD_SYMBOLS[] =
|
||||
{
|
||||
'!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
|
||||
@ -89,6 +91,18 @@ finish:
|
||||
// Passphrase generator
|
||||
// ==========================================================================
|
||||
|
||||
#define NEXT_RANDOM(OUTVAR,LIMIT) do \
|
||||
{ \
|
||||
uint64_t _rnd; \
|
||||
if (slunkcrypt_generate_nonce(&_rnd) != SLUNKCRYPT_SUCCESS) \
|
||||
{ \
|
||||
FPUTS(T("Error: Failed to generate next random number!\n\n"), stderr); \
|
||||
goto clean_up; \
|
||||
} \
|
||||
OUTVAR = (size_t) (_rnd % (LIMIT)); \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
int weak_passphrase(const char *str)
|
||||
{
|
||||
unsigned int flags = 0U;
|
||||
@ -110,26 +124,50 @@ int weak_passphrase(const char *str)
|
||||
int generate_passphrase(const size_t length)
|
||||
{
|
||||
int result = EXIT_FAILURE;
|
||||
char *buffer = NULL, *passwd_symbols = NULL;
|
||||
size_t i, j, n, m;
|
||||
const size_t passwd_len = BOUND(SLUNKCRYPT_PWDLEN_MIN, length, SLUNKCRYPT_PWDLEN_MAX);
|
||||
|
||||
char *const buffer = (char*) malloc((passwd_len + 1U) * sizeof(char));
|
||||
if (!buffer)
|
||||
if (!(buffer = (char*) malloc((passwd_len + 1U) * sizeof(char))))
|
||||
{
|
||||
FPUTS(T("Error: Failed to allocate memory buffer!\n\n"), stderr);
|
||||
return EXIT_FAILURE;
|
||||
goto clean_up;
|
||||
}
|
||||
|
||||
if (!(passwd_symbols = (char*) malloc(ARRAY_SIZE(PASSWD_SYMBOLS) * sizeof(char))))
|
||||
{
|
||||
FPUTS(T("Error: Failed to allocate memory buffer!\n\n"), stderr);
|
||||
goto clean_up;
|
||||
}
|
||||
|
||||
for (i = 0U; i < ARRAY_SIZE(PASSWD_SYMBOLS); ++i)
|
||||
{
|
||||
NEXT_RANDOM(j, i + 1U);
|
||||
if (j != i)
|
||||
{
|
||||
passwd_symbols[i] = passwd_symbols[j];
|
||||
}
|
||||
passwd_symbols[j] = PASSWD_SYMBOLS[i];
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
for (size_t i = 0U; i < passwd_len; ++i)
|
||||
for (n = 0U; n < passwd_len; ++n)
|
||||
{
|
||||
uint64_t value;
|
||||
if (slunkcrypt_generate_nonce(&value) != SLUNKCRYPT_SUCCESS)
|
||||
for (m = 0U; m < MIX_ROUNDS; ++m)
|
||||
{
|
||||
FPUTS(T("Error: Failed to generate next random number!\n\n"), stderr);
|
||||
goto clean_up;
|
||||
for (i = ARRAY_SIZE(PASSWD_SYMBOLS) - 1U; i > 0; --i)
|
||||
{
|
||||
NEXT_RANDOM(j, i + 1U);
|
||||
if (j != i)
|
||||
{
|
||||
const char tmpval = passwd_symbols[i];
|
||||
passwd_symbols[i] = passwd_symbols[j];
|
||||
passwd_symbols[j] = tmpval;
|
||||
}
|
||||
buffer[i] = PASSWD_SYMBOLS[value % ARRAY_SIZE(PASSWD_SYMBOLS)];
|
||||
}
|
||||
}
|
||||
buffer[n] = passwd_symbols[0U];
|
||||
}
|
||||
buffer[passwd_len] = '\0';
|
||||
}
|
||||
@ -147,5 +185,11 @@ clean_up:
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
if (passwd_symbols)
|
||||
{
|
||||
slunkcrypt_bzero(passwd_symbols, ARRAY_SIZE(PASSWD_SYMBOLS) * sizeof(char));
|
||||
free(passwd_symbols);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user