ObjFW  Check-in [0c72729961]

Overview
Comment:ofhash: Allow calculating multiple hashes at once
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0c72729961ee78b5e5385b2e026257ddc510bcd206725e7ada200c373425a052
User & Date: js on 2019-03-21 23:29:28
Other Links: manifest | tags
Context
2019-03-23
14:44
ofhash: Add language dir before parsing options check-in: df94d134d9 user: js tags: trunk
2019-03-21
23:29
ofhash: Allow calculating multiple hashes at once check-in: 0c72729961 user: js tags: trunk
2019-03-20
21:04
Force inline tryReadBits and of_huffman_tree_walk check-in: 6a0bf8be32 user: js tags: trunk
Changes

Modified utils/ofhash/OFHash.m from [c8c16d0702] to [da64d5a676].

18
19
20
21
22
23
24

25
26
27
28
29
30
31
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32







+







#include "config.h"

#import "OFApplication.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFLocale.h"
#import "OFMD5Hash.h"
#import "OFOptionsParser.h"
#import "OFRIPEMD160Hash.h"
#import "OFSHA1Hash.h"
#import "OFSHA224Hash.h"
#import "OFSHA256Hash.h"
#import "OFSHA384Hash.h"
#import "OFSHA512Hash.h"
#import "OFSandbox.h"
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
75
76
77
78
79
80
81






















82













83
84
85
86











87
88
89
90
91














92

93

94
95
96
97
98
99

100
101
102
103
104
105
106
107
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
147
148
149
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160

161
162
163
164
165

166
167
168


169
170
171









172
173
174
175
176
177
178







-
-
+
+





-
-
+
+

-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-

-
+





-
-
-

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
-
+





-
+
-
-
-
-
-


-
-
















-
+
+
+


-
+


-
-
+
+

-
-
-
-
-
-
-
-
-








OF_APPLICATION_DELEGATE(OFHash)

static void
help(void)
{
	[of_stderr writeLine: OF_LOCALIZED(@"usage",
	    @"Usage: %[prog] [md5|rmd160|sha1|sha224|sha256|sha384|sha512] "
	    @"file1 [file2 ...]",
	    @"Usage: %[prog] [--md5|--ripemd160|--sha1|--sha224|--sha256|"
	    @"--sha384|--sha512] file1 [file2 ...]",
	    @"prog", [OFApplication programName])];

	[OFApplication terminateWithStatus: 1];
}

static id <OFCryptoHash>
hashForName(OFString *name)
static void
printHash(OFString *algo, OFString *path, id <OFCryptoHash> hash)
{
	if ([name isEqual: @"md5"])
		return [OFMD5Hash cryptoHash];
	else if ([name isEqual: @"rmd160"] || [name isEqual: @"ripemd160"])
	const unsigned char *digest = hash.digest;
	size_t digestSize = hash.digestSize;

		return [OFRIPEMD160Hash cryptoHash];
	else if ([name isEqual: @"sha1"])
		return [OFSHA1Hash cryptoHash];
	else if ([name isEqual: @"sha224"])
	[of_stdout writeFormat: @"%@ ", algo];

	for (size_t i = 0; i < digestSize; i++)
		[of_stdout writeFormat: @"%02x", digest[i]];
		return [OFSHA224Hash cryptoHash];
	else if ([name isEqual: @"sha256"])
		return [OFSHA256Hash cryptoHash];
	else if ([name isEqual: @"sha384"])
		return [OFSHA384Hash cryptoHash];
	else if ([name isEqual: @"sha512"])
		return [OFSHA512Hash cryptoHash];

	return nil;
	[of_stdout writeFormat: @"  %@\n", path];
}

@implementation OFHash
- (void)applicationDidFinishLaunching
{
	OFArray OF_GENERIC(OFString *) *arguments = [OFApplication arguments];
	id <OFCryptoHash> hash;
	bool first = true;
	int exitStatus = 0;
	bool calculateMD5, calculateRIPEMD160, calculateSHA1, calculateSHA224;
	bool calculateSHA256, calculateSHA384, calculateSHA512;
	const of_options_parser_option_t options[] = {
		{ '\0', @"md5", 0, &calculateMD5, NULL },
		{ '\0', @"ripemd160", 0, &calculateRIPEMD160, NULL },
		{ '\0', @"sha1", 0, &calculateSHA1, NULL },
		{ '\0', @"sha224", 0, &calculateSHA224, NULL },
		{ '\0', @"sha256", 0, &calculateSHA256, NULL },
		{ '\0', @"sha384", 0, &calculateSHA384, NULL },
		{ '\0', @"sha512", 0, &calculateSHA512, NULL },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: options];
	of_unichar_t option;
	OFMD5Hash *MD5Hash = nil;
	OFRIPEMD160Hash *RIPEMD160Hash = nil;
	OFSHA1Hash *SHA1Hash = nil;
	OFSHA224Hash *SHA224Hash = nil;
	OFSHA256Hash *SHA256Hash = nil;
	OFSHA384Hash *SHA384Hash = nil;
	OFSHA512Hash *SHA512Hash = nil;

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case '?':
			if (optionsParser.lastLongOption != nil)
				[of_stderr writeLine:
				    OF_LOCALIZED(@"unknown_long_option",
				    @"%[prog]: Unknown option: --%[opt]",
				    @"prog", [OFApplication programName],
				    @"opt", optionsParser.lastLongOption)];
			else {
				OFString *optStr = [OFString stringWithFormat:
				    @"%c", optionsParser.lastOption];
				[of_stderr writeLine:
#ifdef OF_HAVE_SANDBOX
	OFSandbox *sandbox;

	/*
				    OF_LOCALIZED(@"unkown_option",
				    @"%[prog]: Unknown option: -%[opt]",
				    @"prog", [OFApplication programName],
				    @"opt", optStr)];
			}

			[OFApplication terminateWithStatus: 1];
			break;
		}
	}

	 * SHA-512 is the largest hash supported, so no matter which hash will
	 * be used in the end, this is enough secure memory.
	 */
	[OFSecureData preallocateMemoryWithSize:
	    class_getInstanceSize([OFSHA512Hash class])];
	if (calculateMD5)
		MD5Hash = [OFMD5Hash cryptoHash];
	if (calculateRIPEMD160)
		RIPEMD160Hash = [OFRIPEMD160Hash cryptoHash];
	if (calculateSHA1)
		SHA1Hash = [OFSHA1Hash cryptoHash];
	if (calculateSHA224)
		SHA224Hash = [OFSHA224Hash cryptoHash];
	if (calculateSHA256)
		SHA256Hash = [OFSHA256Hash cryptoHash];
	if (calculateSHA384)
		SHA384Hash = [OFSHA384Hash cryptoHash];
	if (calculateSHA512)
		SHA512Hash = [OFSHA512Hash cryptoHash];

#ifdef OF_HAVE_SANDBOX
	sandbox = [[OFSandbox alloc] init];
	OFSandbox *sandbox = [OFSandbox sandbox];
	@try {
		sandbox.allowsStdIO = true;
		sandbox.allowsReadingFiles = true;
		sandbox.allowsUserDatabaseReading = true;

		for (OFString *path in arguments) {
		for (OFString *path in optionsParser.remainingArguments)
			if (first) {
				first = false;
				continue;
			}

			[sandbox unveilPath: path
				permissions: @"r"];
		}
		first = true;

		[sandbox unveilPath: @LANGUAGE_DIR
			permissions: @"r"];

		[OFApplication activateSandbox: sandbox];
	} @finally {
		[sandbox release];
	}
#endif

#ifndef OF_AMIGAOS
	[OFLocale addLanguageDirectory: @LANGUAGE_DIR];
#else
	[OFLocale addLanguageDirectory: @"PROGDIR:/share/ofhash/lang"];
#endif

	if (arguments.count < 2)
	if (!calculateMD5 && !calculateRIPEMD160 && !calculateSHA1 &&
	    !calculateSHA224 && !calculateSHA256 && !calculateSHA384 &&
	    !calculateSHA512)
		help();

	if ((hash = hashForName(arguments.firstObject)) == nil)
	if (optionsParser.remainingArguments.count < 1)
		help();

	for (OFString *path in arguments) {
		void *pool;
	for (OFString *path in optionsParser.remainingArguments) {
		void *pool = objc_autoreleasePoolPush();
		OFStream *file;
		const uint8_t *digest;
		size_t digestSize;

		if (first) {
			first = false;
			continue;
		}

		pool = objc_autoreleasePoolPush();

		if ([path isEqual: @"-"])
			file = of_stdin;
		else {
			@try {
				file = [OFFile fileWithPath: path
						       mode: @"r"];
159
160
161
162
163
164
165
166







167
168
169
170
171
172
173
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202
203
204
205
206
207
208







-
+
+
+
+
+
+
+







				    @"error", error)];

				exitStatus = 1;
				goto outer_loop_end;
			}
		}

		[hash reset];
		[MD5Hash reset];
		[RIPEMD160Hash reset];
		[SHA1Hash reset];
		[SHA224Hash reset];
		[SHA256Hash reset];
		[SHA384Hash reset];
		[SHA512Hash reset];

		while (!file.atEndOfStream) {
			uint8_t buffer[1024];
			size_t length;

			@try {
				length = [file readIntoBuffer: buffer
183
184
185
186
187
188
189

190
191




















192
193
194
195

196

197
198

199
200
201
202











203
204
205
206
207
208
209
210
218
219
220
221
222
223
224
225


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

251


252




253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271







+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




+
-
+
-
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+








				    @"file", path,
				    @"error", error)];

				exitStatus = 1;
				goto outer_loop_end;
			}

			if (calculateMD5)
			[hash updateWithBuffer: buffer
					length: length];
				[MD5Hash updateWithBuffer: buffer
						   length: length];
			if (calculateRIPEMD160)
				[RIPEMD160Hash updateWithBuffer: buffer
							 length: length];
			if (calculateSHA1)
				[SHA1Hash updateWithBuffer: buffer
						    length: length];
			if (calculateSHA224)
				[SHA224Hash updateWithBuffer: buffer
						      length: length];
			if (calculateSHA256)
				[SHA256Hash updateWithBuffer: buffer
						      length: length];
			if (calculateSHA384)
				[SHA384Hash updateWithBuffer: buffer
						      length: length];
			if (calculateSHA512)
				[SHA512Hash updateWithBuffer: buffer
						      length: length];
		}

		[file close];

		if (calculateMD5)
		digest = hash.digest;
			printHash(@"MD5", path, MD5Hash);
		digestSize = hash.digestSize;

		if (calculateRIPEMD160)
		for (size_t i = 0; i < digestSize; i++)
			[of_stdout writeFormat: @"%02x", digest[i]];

		[of_stdout writeFormat: @"  %@\n", path];
			printHash(@"RIPEMD160", path, RIPEMD160Hash);
		if (calculateSHA1)
			printHash(@"SHA1", path, SHA1Hash);
		if (calculateSHA224)
			printHash(@"SHA224", path, SHA224Hash);
		if (calculateSHA256)
			printHash(@"SHA256", path, SHA256Hash);
		if (calculateSHA384)
			printHash(@"SHA384", path, SHA384Hash);
		if (calculateSHA512)
			printHash(@"SHA512", path, SHA512Hash);

outer_loop_end:
		objc_autoreleasePoolPop(pool);
	}

	[OFApplication terminateWithStatus: exitStatus];
}
@end

Modified utils/ofhash/lang/de.json from [c22df5828c] to [775fc52ec2].

1
2
3
4


5


6
7
8
1
2


3
4
5
6
7
8
9
10


-
-
+
+

+
+



{
    "usage": [
        "Benutzung: %[prog] [md5|rmd160|sha1|sha224|sha256|sha384|sha512] ",
	"datei1 [datei2 ...]"
        "Benutzung: %[prog] [--md5|--ripemd160|--sha1|--sha224|--sha256|",
        "--sha384|--sha512] datei1 [datei2 ...]"
    ],
    "unknown_long_option": "%[prog]: Unbekannte Option: --%[opt]",
    "unknown_option": "%[prog]: Unbekannte Option: -%[opt]",
    "failed_to_open_file": "Fehler beim Öffnen der Datei %[file]: %[error]",
    "failed_to_read_file": "Fehler beim Lesen der Datei %[file]: %[error]"
}