@@ -21,10 +21,11 @@ */ #include #import "ScryptPWGen.h" +#import "NewPasswordGenerator.h" #import "LegacyPasswordGenerator.h" OF_APPLICATION_DELEGATE(ScryptPWGen) static void @@ -37,10 +38,12 @@ [output writeString: @"\n" @"Options:\n" @" -h --help Show this help\n" @" -l --length Length for the derived password\n" + @" -L --legacy Use the legacy algorithm " + @"(compatible with scrypt-genpass)\n" @" -r --repeat Repeat input\n"]; } @implementation ScryptPWGen - (void)applicationDidFinishLaunching @@ -47,17 +50,17 @@ { OFString *lengthStr; const of_options_parser_option_t options[] = { { 'h', @"help", 0, NULL, NULL }, { 'l', @"length", 1, NULL, &lengthStr }, + { 'L', @"legacy", 0, &_legacy, NULL }, { 'r', @"repeat", 0, &_repeat, NULL }, { '\0', nil, 0, NULL, NULL } }; OFOptionsParser *optionsParser = [OFOptionsParser parserWithOptions: options]; of_unichar_t option; - size_t length; char *passphrase; OFString *site, *prompt; while ((option = [optionsParser nextOption]) != '\0') { switch (option) { @@ -96,39 +99,45 @@ [OFApplication terminateWithStatus: 1]; break; } } + if ([[optionsParser remainingArguments] count] != 1) { + showHelp(of_stderr, false); + + [OFApplication terminateWithStatus: 1]; + } + + site = [[optionsParser remainingArguments] firstObject]; + prompt = [OFString stringWithFormat: @"Passphrase for site \"%@\": ", + site]; + + id generator = (_legacy + ? [LegacyPasswordGenerator generator] + : [NewPasswordGenerator generator]); + generator.site = [[optionsParser remainingArguments] firstObject]; + if (lengthStr != nil) { - @try { - length = (size_t)[lengthStr decimalValue]; + bool invalid = false; - if (length < 3) - @throw [OFInvalidFormatException exception]; + @try { + generator.length = (size_t)[lengthStr decimalValue]; + } @catch (OFInvalidArgumentException *e) { + invalid = true; } @catch (OFInvalidFormatException *e) { + invalid = true; + } + + if (invalid) { [of_stderr writeFormat: @"%@: Invalid length: %@\n", [OFApplication programName], lengthStr]; [OFApplication terminateWithStatus: 1]; } } - if ([[optionsParser remainingArguments] count] != 1) { - showHelp(of_stderr, false); - - [OFApplication terminateWithStatus: 1]; - } - - prompt = [OFString stringWithFormat: @"Passphrase for site \"%@\": ", - site]; - - LegacyPasswordGenerator *generator = - [LegacyPasswordGenerator generator]; - generator.length = length; - generator.site = [[optionsParser remainingArguments] firstObject]; - passphrase = getpass( [prompt cStringWithEncoding: [OFSystemInfo native8BitEncoding]]); @try { generator.passphrase = passphrase;