ObjFW  Check-in [b1cd76a8cd]

Overview
Comment:OFCryptoHash: Conform to OFCopying
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b1cd76a8cdba76500c77a57cfd2e8211c61e1d340f3520c72c1be5d026fc17ad
User & Date: js on 2016-07-24 18:54:10
Other Links: manifest | tags
Context
2016-07-24
20:05
OF*Hash: Call [self reset] in -[dealloc] check-in: 096dd743b5 user: js tags: trunk
18:54
OFCryptoHash: Conform to OFCopying check-in: b1cd76a8cd user: js tags: trunk
12:14
-[OFCryptoHash digest]: uint8_t -> unsigned char check-in: 19f7dc67af user: js tags: trunk
Changes

Modified src/OFCryptoHash.h from [91b722cbde] to [7974c3e9f8].

18
19
20
21
22
23
24




25
26

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

30
31
32
33
34
35
36
37







+
+
+
+

-
+








OF_ASSUME_NONNULL_BEGIN

/*!
 * @protocol OFCryptoHash OFCryptoHash.h ObjFW/OFCryptoHash.h
 *
 * @brief A protocol for classes providing cryptographic hash functions.
 *
 * A cryptographic hash implementing this protocol can be copied. The entire
 * state is copied, allowing to calculate a new hash from there. This is
 * especially useful for generating many hashes with a common prefix.
 */
@protocol OFCryptoHash <OFObject>
@protocol OFCryptoHash <OFObject, OFCopying>
/*!
 * A boolean whether the hash has already been calculated.
 */
@property (readonly, getter=isCalculated) bool calculated;

/*!
 * @brief Creates a new cryptographic hash.

Modified src/OFMD5Hash.m from [8b5373705c] to [273d709ad9].

134
135
136
137
138
139
140













141
142
143
144
145
146
147
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







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







{
	self = [super init];

	[self OF_resetState];

	return self;
}

- copy
{
	OFMD5Hash *copy = [[OFMD5Hash alloc] init];

	memcpy(copy->_state, _state, sizeof(_state));
	copy->_bits = _bits;
	memcpy(&copy->_buffer, &_buffer, sizeof(_buffer));
	copy->_bufferLength = _bufferLength;
	copy->_calculated = _calculated;

	return copy;
}

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

Modified src/OFRIPEMD160Hash.m from [6115116dd0] to [d2d42ba8eb].

148
149
150
151
152
153
154













155
156
157
158
159
160
161
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







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







{
	self = [super init];

	[self OF_resetState];

	return self;
}

- copy
{
	OFRIPEMD160Hash *copy = [[OFRIPEMD160Hash alloc] init];

	memcpy(copy->_state, _state, sizeof(_state));
	copy->_bits = _bits;
	memcpy(&copy->_buffer, &_buffer, sizeof(_buffer));
	copy->_bufferLength = _bufferLength;
	copy->_calculated = _calculated;

	return copy;
}

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

Modified src/OFSHA1Hash.m from [e1d7855925] to [32e9d453f3].

108
109
110
111
112
113
114













115
116
117
118
119
120
121
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







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







{
	self = [super init];

	[self OF_resetState];

	return self;
}

- copy
{
	OFSHA1Hash *copy = [[OFSHA1Hash alloc] init];

	memcpy(copy->_state, _state, sizeof(_state));
	copy->_bits = _bits;
	memcpy(&copy->_buffer, &_buffer, sizeof(_buffer));
	copy->_bufferLength = _bufferLength;
	copy->_calculated = _calculated;

	return copy;
}

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

Modified src/OFSHA224Or256Hash.m from [2324ca56a2] to [3f98abf1a7].

140
141
142
143
144
145
146













147
148
149
150
151
152
153
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







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







	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- copy
{
	OFSHA224Or256Hash *copy = [[[self class] alloc] init];

	memcpy(copy->_state, _state, sizeof(_state));
	copy->_bits = _bits;
	memcpy(&copy->_buffer, &_buffer, sizeof(_buffer));
	copy->_bufferLength = _bufferLength;
	copy->_calculated = _calculated;

	return copy;
}

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

	if (_calculated)

Modified src/OFSHA384Or512Hash.m from [5c6c4dfe45] to [775711ae59].

151
152
153
154
155
156
157













158
159
160
161
162
163
164
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







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







	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- copy
{
	OFSHA384Or512Hash *copy = [[[self class] alloc] init];

	memcpy(copy->_state, _state, sizeof(_state));
	memcpy(copy->_bits, _bits, sizeof(_bits));
	memcpy(&copy->_buffer, &_buffer, sizeof(_buffer));
	copy->_bufferLength = _bufferLength;
	copy->_calculated = _calculated;

	return copy;
}

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

	if (_calculated)

Modified tests/OFMD5HashTests.m from [d66f994271] to [bfa03836eb].

32
33
34
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
32
33
34
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







-
+














+
+
+
-
+
+









const uint8_t testfile_md5[16] =
	"\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38";

@implementation TestsAppDelegate (OFMD5HashTests)
- (void)MD5HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *md5;
	OFMD5Hash *md5, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (md5 = [OFMD5Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readIntoBuffer: buf
					length: 64];
		[md5 updateWithBuffer: buf
			       length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[md5 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([md5 digest], testfile_md5, 16))
	    memcmp([md5 digest], testfile_md5, 16) == 0 &&
	    memcmp([copy digest], testfile_md5, 16) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length]", OFHashAlreadyCalculatedException,
	    [md5 updateWithBuffer: ""
			   length: 1])

	[pool drain];
}
@end

Modified tests/OFRIPEMD160HashTests.m from [77ab460e7a] to [5e5300e24a].

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







-
+














+
+
+
-
+
+









	"\x46\x02\x97\xF5\x85\xDF\xB9\x21\x00\xC8\xF9\x87\xC6\xEC\x84\x0D\xCE"
	"\xE6\x08\x8B";

@implementation TestsAppDelegate (OFRIPEMD160HashTests)
- (void)RIPEMD160HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFRIPEMD160Hash *rmd160;
	OFRIPEMD160Hash *rmd160, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (rmd160 = [OFRIPEMD160Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readIntoBuffer: buf
					length: 64];
		[rmd160 updateWithBuffer: buf
				  length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[rmd160 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([rmd160 digest], testfile_rmd160, 20))
	    memcmp([rmd160 digest], testfile_rmd160, 20) == 0 &&
	    memcmp([copy digest], testfile_rmd160, 20) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length]", OFHashAlreadyCalculatedException,
	    [rmd160 updateWithBuffer: ""
			      length: 1])

	[pool drain];
}
@end

Modified tests/OFSHA1HashTests.m from [283cdacad0] to [2f05888fd4].

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







-
+














+
+
+
-
+
+









	"\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5"
	"\x94\xE7\x17";

@implementation TestsAppDelegate (SHA1HashTests)
- (void)SHA1HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA1Hash *sha1;
	OFSHA1Hash *sha1, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (sha1 = [OFSHA1Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readIntoBuffer: buf
					length: 64];
		[sha1 updateWithBuffer: buf
				length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[sha1 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([sha1 digest], testfile_sha1, 20))
	    memcmp([sha1 digest], testfile_sha1, 20) == 0 &&
	    memcmp([copy digest], testfile_sha1, 20) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException,
	    [sha1 updateWithBuffer: ""
			    length: 1])

	[pool drain];
}
@end

Modified tests/OFSHA224HashTests.m from [c912281416] to [dbfecb02c9].

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







-
+














+
+
+
-
+
+









	"\x27\x69\xD8\x04\x2D\x0F\xCA\x84\x6C\xF1\x62\x44\xBA\x0C\xBD\x46\x64"
	"\x5F\x4F\x20\x02\x4D\x15\xED\x1C\x61\x1F\xF7";

@implementation TestsAppDelegate (SHA224HashTests)
- (void)SHA224HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA224Hash *sha224;
	OFSHA224Hash *sha224, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (sha224 = [OFSHA224Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readIntoBuffer: buf
					length: 64];
		[sha224 updateWithBuffer: buf
				  length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[sha224 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([sha224 digest], testfile_sha224, 28))
	    memcmp([sha224 digest], testfile_sha224, 28) == 0 &&
	    memcmp([copy digest], testfile_sha224, 28) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException,
	    [sha224 updateWithBuffer: ""
			      length: 1])

	[pool drain];
}
@end

Modified tests/OFSHA256HashTests.m from [fa3f9610d1] to [2cdb5f3a64].

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







-
+














+
+
+
-
+
+









	"\x1A\x02\xD6\x46\xF5\xA6\xBA\xAA\xFF\x7F\xD5\x87\xBA\xC3\xF6\xC6\xB5"
	"\x67\x93\x8F\x0F\x44\x90\xB8\xF5\x35\x89\xF0\x5A\x23\x7F\x69";

@implementation TestsAppDelegate (SHA256HashTests)
- (void)SHA256HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA256Hash *sha256;
	OFSHA256Hash *sha256, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (sha256 = [OFSHA256Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readIntoBuffer: buf
					length: 64];
		[sha256 updateWithBuffer: buf
				  length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[sha256 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([sha256 digest], testfile_sha256, 32))
	    memcmp([sha256 digest], testfile_sha256, 32) == 0 &&
	    memcmp([copy digest], testfile_sha256, 32) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException,
	    [sha256 updateWithBuffer: ""
			      length: 1])

	[pool drain];
}
@end

Modified tests/OFSHA384HashTests.m from [d990425313] to [d55ef8c208].

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







-
+














+
+
+
-
+
+









	"\x94\x1B\xD2\x78\x5C\xCF\xF3\x8A\xB8\x98\x22\xC7\x0E\xFE\xF1\xEC\x53"
	"\xE9\x1A\xB3\x51\x70\x8C\x1F\x3F\x56\x12\x44\x01\x91\x54";

@implementation TestsAppDelegate (SHA384HashTests)
- (void)SHA384HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA384Hash *sha384;
	OFSHA384Hash *sha384, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (sha384 = [OFSHA384Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[128];
		size_t len = [f readIntoBuffer: buf
					length: 128];
		[sha384 updateWithBuffer: buf
				  length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[sha384 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([sha384 digest], testfile_sha384, 48))
	    memcmp([sha384 digest], testfile_sha384, 48) == 0 &&
	    memcmp([copy digest], testfile_sha384, 48) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException,
	    [sha384 updateWithBuffer: ""
			      length: 1])

	[pool drain];
}
@end

Modified tests/OFSHA512HashTests.m from [d5695d5fe1] to [a6a9498b8b].

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







-
+














+
+
+
-
+
+









	"\xB1\x30\x97\x82\x58\xA5\x1F\x71\x42\xE6\x56\x07\x99\x57\xB2\xB8\x3B"
	"\xA1\x8A\x41\x64\x33\x69\x21\x8C\x2A\x44\x6D\xF2\xA0";

@implementation TestsAppDelegate (SHA512HashTests)
- (void)SHA512HashTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA512Hash *sha512;
	OFSHA512Hash *sha512, *copy;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[cryptoHash]", (sha512 = [OFSHA512Hash cryptoHash]))

	while (![f isAtEndOfStream]) {
		char buf[128];
		size_t len = [f readIntoBuffer: buf
					length: 128];
		[sha512 updateWithBuffer: buf
				  length: len];
	}
	[f close];

	TEST(@"-[copy]", (copy = [[sha512 copy] autorelease]))

	TEST(@"-[digest]",
	TEST(@"-[digest]", !memcmp([sha512 digest], testfile_sha512, 64))
	    memcmp([sha512 digest], testfile_sha512, 64) == 0 &&
	    memcmp([copy digest], testfile_sha512, 64) == 0)

	EXPECT_EXCEPTION(@"Detect invalid call of "
	    @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException,
	    [sha512 updateWithBuffer: ""
			      length: 1])

	[pool drain];
}
@end