From 0bde28284ae132bc5a7f0405b87f71aac3431631 Mon Sep 17 00:00:00 2001 From: Robert Hunziker Date: Wed, 19 May 2021 15:49:14 +0200 Subject: [PATCH 1/2] password length changed to 20 --- passgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passgen.c b/passgen.c index 0fe64b9..615cf17 100644 --- a/passgen.c +++ b/passgen.c @@ -41,7 +41,7 @@ /* An implementation of memset() that the compiler won't optimize out. */ #include "libs/memset_s.h" -#define PASSWORD_LENGTH 64 +#define PASSWORD_LENGTH 20 #define WORD_COUNT 10 #define CHARSET_HEX "0123456789ABCDEF" From 738450451e888e469a98869e7f5e4bed92a107d3 Mon Sep 17 00:00:00 2001 From: osgard1 Date: Wed, 19 May 2021 16:29:50 +0200 Subject: [PATCH 2/2] parameter for password length added. --- passgen.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/passgen.c b/passgen.c index 615cf17..189718c 100644 --- a/passgen.c +++ b/passgen.c @@ -41,7 +41,6 @@ /* An implementation of memset() that the compiler won't optimize out. */ #include "libs/memset_s.h" -#define PASSWORD_LENGTH 20 #define WORD_COUNT 10 #define CHARSET_HEX "0123456789ABCDEF" @@ -68,6 +67,7 @@ static struct option long_options[] = { {"lower", no_argument, NULL, 'l' }, {"words", no_argument, NULL, 'w' }, {"password-count", required_argument, NULL, 'p' }, + {"password-length", required_argument, NULL, 'q' }, /* This skips the self test -- don't do it unless you're testing. */ {"dont-use-this", no_argument, NULL, 'z' }, {NULL, 0, NULL, 0 } @@ -78,6 +78,7 @@ int main(int argc, char* argv[]) /* Options */ const char *set; int numberOfPasswords = 1; + int passwordLength = 20; int generateWordPassword = 0; int skipSelfTest = 0; @@ -85,7 +86,8 @@ int main(int argc, char* argv[]) int optionCharacter = 0; int isPasswordTypeSet = 0; int isPasswordCountSet = 0; - while((optionCharacter = getopt_long(argc, argv, "hzxndlwap:", long_options, NULL)) != -1) { + int isPasswordLengthSet = 0; + while((optionCharacter = getopt_long(argc, argv, "hzxndlwap:q:", long_options, NULL)) != -1) { switch(optionCharacter) { case 'h': /* help */ @@ -163,6 +165,23 @@ int main(int argc, char* argv[]) } break; + case 'q': /* password length */ + if (isPasswordLengthSet) { + showHelp(); + return EXIT_FAILURE; + } + if(sscanf(optarg, "%10d", &passwordLength) == 1) { + if (passwordLength < 1 || passwordLength > 256) { + showHelp(); + return EXIT_FAILURE; + } + isPasswordLengthSet = 1; + } else { + showHelp(); + return EXIT_FAILURE; + } + break; + case 'z': /* skip self test - for test.rb */ skipSelfTest = 1; break; @@ -195,18 +214,18 @@ int main(int argc, char* argv[]) } } } else { - unsigned char result[PASSWORD_LENGTH]; + unsigned char result[passwordLength]; for(int i = 0; i < numberOfPasswords; i++) { - if(getPassword(set, strlen(set), result, PASSWORD_LENGTH)) { - fwrite(result, sizeof(unsigned char), PASSWORD_LENGTH, stdout); + if(getPassword(set, strlen(set), result, passwordLength)) { + fwrite(result, sizeof(unsigned char), passwordLength, stdout); printf("\n"); } else { - memset_s(result, 0, PASSWORD_LENGTH); + memset_s(result, 0, passwordLength); fprintf(stderr, "Error getting random data or allocating memory.\n"); return EXIT_FAILURE; } - memset_s(result, 0, PASSWORD_LENGTH); + memset_s(result, 0, passwordLength); } } @@ -227,6 +246,7 @@ void showHelp(void) puts("Where can be:"); puts(" -p, --password-count N\t\tSpecify number of passwords to generate"); + puts(" -q, --password-length N\t\tSpecify length of passwords to generate (1-256)"); puts("WARNING: If automated, you MUST check that the exit status is 0."); }