ObjFW  Check-in [7c27db9fe0]

Overview
Comment:Add -[OFHash reset]

This is useful to calculate many hashes without allocating new objects.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7c27db9fe06808c2d4b35574e4181be9b090b95e0605bb01ff714ca9f9d1151c
User & Date: js on 2015-02-01 00:47:18
Other Links: manifest | tags
Context
2015-02-01
00:47
Doxyfile: Define OF_NO_RETURN check-in: f5dce70abd user: js tags: trunk
00:47
Add -[OFHash reset] check-in: 7c27db9fe0 user: js tags: trunk
2015-01-31
11:46
OF{MD5,RIPEMD160,SHA*}Hash: Small optimization check-in: c56b388459 user: js tags: trunk
Changes

Modified src/OFHash.h from [a9b39a0eb7] to [3407d4c3db].

68
69
70
71
72
73
74









75
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84







+
+
+
+
+
+
+
+
+


/*!
 * @brief Returns a boolean whether the hash has already been calculated.
 *
 * @return A boolean whether the hash has already been calculated
 */
- (bool)isCalculated;

/*!
 * @brief Resets all state so that a new hash can be calculated.
 *
 * @warning This invalidates any pointer previously returned by @ref digest. If
 *	    you are still interested in the previous digest, you need to memcpy
 *	    it yourself before calling @ref reset!
 */
- (void)reset;
@end

Modified src/OFMD5Hash.h from [03022e8a95] to [f6743c94bf].

28
29
30
31
32
33
34


35
28
29
30
31
32
33
34
35
36
37







+
+

	union {
		uint8_t bytes[64];
		uint32_t words[16];
	} _buffer;
	size_t _bufferLength;
	bool _calculated;
}

- (void)OF_resetState;
@end

Modified src/OFMD5Hash.m from [4656f6b7f6] to [3333d71ead].

130
131
132
133
134
135
136







137
138
139
140
141
142
143
144
145
146
147
148
149
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







+
+
+
+
+
+
+




-
-







	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0x67452301;
	_state[1] = 0xEFCDAB89;
	_state[2] = 0x98BADCFE;
	_state[3] = 0x10325476;

	return self;
}

- (void)updateWithBuffer: (const void*)buffer_
		  length: (size_t)length
{
	const uint8_t *buffer = buffer_;

196
197
198
199
200
201
202









203
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217







+
+
+
+
+
+
+
+
+

	return (const uint8_t*)_state;
}

- (bool)isCalculated
{
	return _calculated;
}

- (void)reset
{
	[self OF_resetState];
	_bits = 0;
	memset(&_buffer, 0, sizeof(_buffer));
	_bufferLength = 0;
	_calculated = false;
}
@end

Modified src/OFRIPEMD160Hash.h from [f677768429] to [59577d187f].

28
29
30
31
32
33
34


35
28
29
30
31
32
33
34
35
36
37







+
+

	union {
		uint8_t bytes[64];
		uint32_t words[16];
	} _buffer;
	size_t _bufferLength;
	bool _calculated;
}

- (void)OF_resetState;
@end

Modified src/OFRIPEMD160Hash.m from [353a43c133] to [0df0a4dfec].

144
145
146
147
148
149
150







151
152
153
154
155
156
157
158
159
160
161
162
163
164
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







+
+
+
+
+
+
+





-
-







	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0x67452301;
	_state[1] = 0xEFCDAB89;
	_state[2] = 0x98BADCFE;
	_state[3] = 0x10325476;
	_state[4] = 0xC3D2E1F0;

	return self;
}

- (void)updateWithBuffer: (const void*)buffer_
		  length: (size_t)length
{
	const uint8_t *buffer = buffer_;

211
212
213
214
215
216
217









218
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232







+
+
+
+
+
+
+
+
+

	return (const uint8_t*)_state;
}

- (bool)isCalculated
{
	return _calculated;
}

- (void)reset
{
	[self OF_resetState];
	_bits = 0;
	memset(&_buffer, 0, sizeof(_buffer));
	_bufferLength = 0;
	_calculated = false;
}
@end

Modified src/OFSHA1Hash.h from [ad2827a36b] to [f7d1ee4112].

28
29
30
31
32
33
34


35
28
29
30
31
32
33
34
35
36
37







+
+

	union {
		uint8_t bytes[64];
		uint32_t words[80];
	} _buffer;
	size_t _bufferLength;
	bool _calculated;
}

- (void)OF_resetState;
@end

Modified src/OFSHA1Hash.m from [bb076bbf91] to [b45fb37713].

104
105
106
107
108
109
110







111
112
113
114
115
116
117
118
119
120
121
122
123
124
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







+
+
+
+
+
+
+





-
-







	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0x67452301;
	_state[1] = 0xEFCDAB89;
	_state[2] = 0x98BADCFE;
	_state[3] = 0x10325476;
	_state[4] = 0xC3D2E1F0;

	return self;
}

- (void)updateWithBuffer: (const void*)buffer_
		  length: (size_t)length
{
	const uint8_t *buffer = buffer_;

171
172
173
174
175
176
177









178
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192







+
+
+
+
+
+
+
+
+

	return (const uint8_t*)_state;
}

- (bool)isCalculated
{
	return _calculated;
}

- (void)reset
{
	[self OF_resetState];
	_bits = 0;
	memset(&_buffer, 0, sizeof(_buffer));
	_bufferLength = 0;
	_calculated = false;
}
@end

Modified src/OFSHA224Hash.m from [5d213317d0] to [6f53e35d7d].

24
25
26
27
28
29
30







31
32
33
34
35
36
37
38
39

40
41
42
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46


47







+
+
+
+
+
+
+








-
+
-
-

	return 28;
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0xC1059ED8;
	_state[1] = 0x367CD507;
	_state[2] = 0x3070DD17;
	_state[3] = 0xF70E5939;
	_state[4] = 0xFFC00B31;
	_state[5] = 0x68581511;
	_state[6] = 0x64F98FA7;
	_state[7] = 0xBEFA4FA4;

}
	return self;
}
@end

Modified src/OFSHA224Or256Hash.h from [81c7fe9aca] to [257314d339].

28
29
30
31
32
33
34


35
28
29
30
31
32
33
34
35
36
37







+
+

	union {
		uint8_t bytes[64];
		uint32_t words[64];
	} _buffer;
	size_t _bufferLength;
	bool _calculated;
}

- (void)OF_resetState;
@end

Modified src/OFSHA224Or256Hash.m from [4bdaecb076] to [1adbe2ccf4].

196
197
198
199
200
201
202














203
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217







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

	return (const uint8_t*)_state;
}

- (bool)isCalculated
{
	return _calculated;
}

- (void)reset
{
	[self OF_resetState];
	_bits = 0;
	memset(&_buffer, 0, sizeof(_buffer));
	_bufferLength = 0;
	_calculated = false;
}

- (void)OF_resetState
{
	OF_UNRECOGNIZED_SELECTOR
}
@end

Modified src/OFSHA256Hash.m from [5bfe5983a4] to [3137857ab3].

24
25
26
27
28
29
30







31
32
33
34
35
36
37
38
39

40
41
42
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46


47







+
+
+
+
+
+
+








-
+
-
-

	return 32;
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0x6A09E667;
	_state[1] = 0xBB67AE85;
	_state[2] = 0x3C6EF372;
	_state[3] = 0xA54FF53A;
	_state[4] = 0x510E527F;
	_state[5] = 0x9B05688C;
	_state[6] = 0x1F83D9AB;
	_state[7] = 0x5BE0CD19;

}
	return self;
}
@end

Modified src/OFSHA384Hash.m from [843549facd] to [6d0c49039d].

24
25
26
27
28
29
30







31
32
33
34
35
36
37
38
39

40
41
42
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46


47







+
+
+
+
+
+
+








-
+
-
-

	return 48;
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0xCBBB9D5DC1059ED8;
	_state[1] = 0x629A292A367CD507;
	_state[2] = 0x9159015A3070DD17;
	_state[3] = 0x152FECD8F70E5939;
	_state[4] = 0x67332667FFC00B31;
	_state[5] = 0x8EB44A8768581511;
	_state[6] = 0xDB0C2E0D64F98FA7;
	_state[7] = 0x47B5481DBEFA4FA4;

}
	return self;
}
@end

Modified src/OFSHA384Or512Hash.h from [d13d87a9f5] to [468a3cb6e2].

28
29
30
31
32
33
34


35
28
29
30
31
32
33
34
35
36
37







+
+

	union {
		uint8_t bytes[128];
		uint64_t words[80];
	} _buffer;
	size_t _bufferLength;
	bool _calculated;
}

- (void)OF_resetState;
@end

Modified src/OFSHA384Or512Hash.m from [4636315b6a] to [139ebf49f1].

209
210
211
212
213
214
215














216
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230







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

	return (const uint8_t*)_state;
}

- (bool)isCalculated
{
	return _calculated;
}

- (void)reset
{
	[self OF_resetState];
	memset(&_bits, 0, sizeof(_bits));
	memset(&_buffer, 0, sizeof(_buffer));
	_bufferLength = 0;
	_calculated = false;
}

- (void)OF_resetState
{
	OF_UNRECOGNIZED_SELECTOR
}
@end

Modified src/OFSHA512Hash.m from [bc46710b04] to [e761d41b46].

24
25
26
27
28
29
30







31
32
33
34
35
36
37
38
39

40
41
42
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46


47







+
+
+
+
+
+
+








-
+
-
-

	return 64;
}

- init
{
	self = [super init];

	[self OF_resetState];

	return self;
}

- (void)OF_resetState
{
	_state[0] = 0x6A09E667F3BCC908;
	_state[1] = 0xBB67AE8584CAA73B;
	_state[2] = 0x3C6EF372FE94F82B;
	_state[3] = 0xA54FF53A5F1D36F1;
	_state[4] = 0x510E527FADE682D1;
	_state[5] = 0x9B05688C2B3E6C1F;
	_state[6] = 0x1F83D9ABFB41BD6B;
	_state[7] = 0x5BE0CD19137E2179;

}
	return self;
}
@end

Modified utils/ofhash/OFHash.m from [27bb220d00] to [9c55df296d].

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
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







-
-
+
+


-
+

-
+

-
+

-
+

-
+

-
+

-
+








-
+







-
+






-



















+
+




















+



-
+
+












	    @"Usage: %@ [md5|rmd160|sha1|sha224|sha256|sha384|sha512] "
	    @"file1 [file2 ...]\n",
	    [OFApplication programName]];

	[OFApplication terminateWithStatus: 1];
}

static Class
hashClassForName(OFString *name)
static id <OFHash>
hashForName(OFString *name)
{
	if ([name isEqual: @"md5"])
		return [OFMD5Hash class];
		return [OFMD5Hash hash];
	else if ([name isEqual: @"rmd160"] || [name isEqual: @"ripemd160"])
		return [OFRIPEMD160Hash class];
		return [OFRIPEMD160Hash hash];
	else if ([name isEqual: @"sha1"])
		return [OFSHA1Hash class];
		return [OFSHA1Hash hash];
	else if ([name isEqual: @"sha224"])
		return [OFSHA224Hash class];
		return [OFSHA224Hash hash];
	else if ([name isEqual: @"sha256"])
		return [OFSHA256Hash class];
		return [OFSHA256Hash hash];
	else if ([name isEqual: @"sha384"])
		return [OFSHA384Hash class];
		return [OFSHA384Hash hash];
	else if ([name isEqual: @"sha512"])
		return [OFSHA512Hash class];
		return [OFSHA512Hash hash];

	return nil;
}

@implementation OFHash
- (void)applicationDidFinishLaunching
{
	OFArray *arguments = [OFApplication arguments];
	Class <OFHash> hashClass;
	id <OFHash> hash;
	OFEnumerator *enumerator;
	OFString *path;
	int exitStatus = 0;

	if ([arguments count] < 2)
		help();

	if ((hashClass = hashClassForName([arguments objectAtIndex: 0])) == Nil)
	if ((hash = hashForName([arguments firstObject])) == nil)
		help();

	enumerator = [[arguments objectsInRange:
	    of_range(1, [arguments count] - 1)] objectEnumerator];
	while ((path = [enumerator nextObject]) != nil) {
		void *pool = objc_autoreleasePoolPush();
		id <OFHash> hash = [hashClass hash];
		OFStream *file;
		const uint8_t *digest;
		size_t i, digestSize;

		if ([path isEqual: @"-"])
			file = of_stdin;
		else {
			@try {
				file = [OFFile fileWithPath: path
						       mode: @"rb"];
			} @catch (OFOpenFileFailedException *e) {
				[of_stderr writeFormat:
				    @"Failed to open file %@: %s\n",
				    [e path], strerror([e errNo])];

				exitStatus = 1;
				goto outer_loop_end;
			}
		}

		[hash reset];

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

			@try {
				length = [file readIntoBuffer: buffer
						       length: 1024];
			} @catch (OFReadFailedException *e) {
				[of_stderr writeFormat:
				    @"Failed to read %@: %s\n",
				    path, strerror([e errNo])];

				exitStatus = 1;
				goto outer_loop_end;
			}

			[hash updateWithBuffer: buffer
					length: length];
		}

		[file close];

		digest = [hash digest];
		digestSize = [hashClass digestSize];
		digestSize = [[hash class] digestSize];

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

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

outer_loop_end:
		objc_autoreleasePoolPop(pool);
	}

	[OFApplication terminateWithStatus: exitStatus];
}
@end