CryptoPassphrase  Diff

Differences From Artifact [c48c7fa7a2]:

To Artifact [9af2fb9acc]:


35
36
37
38
39
40
41

42
43
44
45
46
47
48
49
50
51

52
53

54

55
56
57
58
59
60
61
62

63


64
65
66
67
68
69
70
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

52
53
54
55

56
57
58
59
60
61
62
63

64
65
66
67
68
69
70
71
72
73
74







+









-
+


+
-
+







-
+

+
+







			     [OFApplication programName]];

	if (verbose)
		[output writeString:
		    @"\n"
		    @"Options:\n"
		    @"    -h  --help    Show this help\n"
		    @"    -k  --keyfile Use the specified key file\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
{
	OFString *lengthStr;
	OFString *keyfilePath, *lengthString;
	const of_options_parser_option_t options[] = {
		{ 'h', @"help", 0, NULL, NULL },
		{ 'k', @"keyfile", 1, NULL, &keyfilePath },
		{ 'l', @"length", 1, NULL, &lengthStr },
		{ 'l', @"length", 1, NULL, &lengthString },
		{ 'L', @"legacy", 0, &_legacy, NULL },
		{ 'r', @"repeat", 0, &_repeat, NULL },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: options];
	of_unichar_t option;
	char *passphrase;
	OFMutableData *keyfile = nil;
	OFString *prompt;
	const char *promptCString;
	char *passphrase;

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case 'h':
			showHelp(of_stdout, true);

			[OFApplication terminate];
108
109
110
111
112
113
114
115

116
117
118
119

120
121
122
123
124
125
126
127
128
129

130
131
132
133
134
135
136
137





138

139
140
141
142
143
144
145
146
112
113
114
115
116
117
118

119
120
121
122

123
124
125
126
127
128
129
130
131
132

133
134
135
136
137
138

139
140
141
142
143
144
145

146

147
148
149
150
151
152
153







-
+



-
+









-
+





-


+
+
+
+
+
-
+
-







	}

	id <PasswordGenerator> generator = (_legacy
	    ? [LegacyPasswordGenerator generator]
	    : [NewPasswordGenerator generator]);
	generator.site = [[optionsParser remainingArguments] firstObject];

	if (lengthStr != nil) {
	if (lengthString != nil) {
		bool invalid = false;

		@try {
			generator.length = (size_t)[lengthStr decimalValue];
			generator.length = (size_t)[lengthString decimalValue];
		} @catch (OFInvalidFormatException *e) {
			invalid = true;
		} @catch (OFOutOfRangeException *e) {
			invalid = true;
		}

		if (invalid) {
			[of_stderr writeFormat:
			    @"%@: Invalid length: %@\n",
			    [OFApplication programName], lengthStr];
			    [OFApplication programName], lengthString];

			[OFApplication terminateWithStatus: 1];
		}
	}


	prompt = [OFString stringWithFormat: @"Passphrase for site \"%@\": ",
					     generator.site];
	promptCString = [prompt cStringWithEncoding: [OFLocalization encoding]];

	if (keyfilePath != nil)
		keyfile = [OFMutableData dataWithContentsOfFile: keyfilePath];

	passphrase = getpass(
	passphrase = getpass(promptCString);
	    [prompt cStringWithEncoding: [OFLocalization encoding]]);
	@try {
		if (_repeat) {
			char *passphraseCopy = of_strdup(passphrase);

			if (passphraseCopy == NULL)
				@throw [OFOutOfMemoryException exception];

162
163
164
165
166
167
168

169
170
171
172
173
174
175
176
177
178
179
180
181
182



183
184
185
186
187
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198







+














+
+
+





			} @finally {
				of_explicit_memset(passphraseCopy, 0,
				    strlen(passphraseCopy));
				free(passphraseCopy);
			}
		}

		generator.keyfile = keyfile;
		generator.passphrase = passphrase;

		[generator derivePassword];
		@try {
			[of_stdout writeBuffer: generator.output
					length: generator.length];
			[of_stdout writeBuffer: "\n"
					length: 1];
		} @finally {
			of_explicit_memset(generator.output, 0,
			    generator.length);
		}
	} @finally {
		of_explicit_memset(passphrase, 0, strlen(passphrase));

		if (keyfile != nil)
			of_explicit_memset([keyfile items], 0, [keyfile count]);
	}

	[OFApplication terminate];
}
@end