-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYDigester.h
152 lines (136 loc) · 4.46 KB
/
SSYDigester.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
#import <Cocoa/Cocoa.h>
#import <CommonCrypto/CommonDigest.h>
enum SSYDigesterAlgorithm_enum {
SSYDigesterAlgorithmMd5,
SSYDigesterAlgorithmSha1,
SSYDigesterAlgorithmSha256,
SSYDigesterAlgorithmSha512,
} ;
typedef enum SSYDigesterAlgorithm_enum SSYDigesterAlgorithm ;
/*!
@brief A class for computing message digests incrementally.
*/
@interface SSYDigester : NSObject {
SSYDigesterAlgorithm m_algorithm ;
CC_MD5_CTX m_context_md5 ;
CC_SHA1_CTX m_context_sha1 ;
CC_SHA256_CTX m_context_sha256 ;
CC_SHA512_CTX m_context_sha512 ;
}
/*!
@brief Initializes a new message digest with a given
algorithm type.
*/
- (id)initWithAlgorithm:(SSYDigesterAlgorithm)algorithm ;
/*!
@brief Incrementally updates the message digest to
account for additional given data.
*/
- (void)updateWithData:(NSData*)data ;
/*!
@brief Incrementally updates the message digest to
account for an additional given string which is first
converted to a C string with a given encoding.
@details The NULL terminator on the C string *is*
included in the additional data.
@param encoding The string encoding to be used to
convert the given string to data. Must be one of the
following:
* NSUTF8StringEncoding
* NSASCIIStringEncoding
* NSUnicodeStringEncoding
* NSUTF16StringEncoding
* NSUTF16BigEndianStringEncoding
* NSUTF16LittleEndianStringEncoding
*/
- (void)updateWithString:(NSString*)string
encoding:(NSStringEncoding)encoding ;
/*!
@brief Returns the final message digest.
@details After sending this message to the receiver,
you should release/deallocate it. Sending a
further message to the receiver afer sending -finalize
for the first time will result in an assertion being
raised.
*/
- (NSData*)finalizeDigest ;
@end
#if 0
/* The following method was used to test this class on 20150304, when it was
converted to use CommonCrypto instead of openssl. The "expected" results
were obtained using the old openssl code. */
+ (void)testSSYDigester {
SSYDigester* digester ;
NSData* data ;
NSData* expected ;
digester = [[SSYDigester alloc] initWithAlgorithm:SSYDigesterAlgorithmMd5] ;
data = [@"Foo One" dataUsingEncoding:NSUTF8StringEncoding] ;
[digester updateWithData:data] ;
[digester updateWithString:@"Foo Two"
encoding:NSUTF8StringEncoding] ;
data = [@"Foo Three" dataUsingEncoding:NSUTF8StringEncoding] ;
[digester updateWithData:data] ;
[digester updateWithString:@"Foo Four!!"
encoding:NSUTF8StringEncoding] ;
data = [digester finalizeDigest] ;
NSLog(@" md5 result = %@", data) ;
[digester release] ;
char bytes[20] ;
bytes[0] = 0x0c ;
bytes[1] = 0xab ;
bytes[2] = 0x25 ;
bytes[3] = 0x8a ;
bytes[4] = 0xbb ;
bytes[5] = 0xc7 ;
bytes[6] = 0x16 ;
bytes[7] = 0xdf ;
bytes[8] = 0x72 ;
bytes[9] = 0x5e ;
bytes[10] = 0x83 ;
bytes[11] = 0xd9 ;
bytes[12] = 0x53 ;
bytes[13] = 0xa8 ;
bytes[14] = 0xaf ;
bytes[15] = 0x21 ;
expected = [[NSData alloc] initWithBytes:bytes
length:16] ;
NSLog(@" md5 expect = %@", expected) ;
NSLog(@"md5 test %@", [data isEqual:expected] ? @"passed" : @"failed") ;
digester = [[SSYDigester alloc] initWithAlgorithm:SSYDigesterAlgorithmSha1] ;
data = [@"Foo One" dataUsingEncoding:NSUTF8StringEncoding] ;
[digester updateWithData:data] ;
[digester updateWithString:@"Foo Two"
encoding:NSUTF8StringEncoding] ;
data = [@"Foo Three" dataUsingEncoding:NSUTF8StringEncoding] ;
[digester updateWithData:data] ;
[digester updateWithString:@"Foo Four!!"
encoding:NSUTF8StringEncoding] ;
data = [digester finalizeDigest] ;
NSLog(@"sha1 result = %@", data) ;
[digester release] ;
bytes[0] = 0x97 ;
bytes[1] = 0x59 ;
bytes[2] = 0xf0 ;
bytes[3] = 0xd1 ;
bytes[4] = 0xc0 ;
bytes[5] = 0x71 ;
bytes[6] = 0xb2 ;
bytes[7] = 0x30 ;
bytes[8] = 0xdd ;
bytes[9] = 0x98 ;
bytes[10] = 0x2c ;
bytes[11] = 0x0a ;
bytes[12] = 0x27 ;
bytes[13] = 0xb3 ;
bytes[14] = 0xff ;
bytes[15] = 0x16 ;
bytes[16] = 0x10 ;
bytes[17] = 0x4a ;
bytes[18] = 0xe9 ;
bytes[19] = 0x33 ;
expected = [[NSData alloc] initWithBytes:bytes
length:20] ;
NSLog(@"sha1 expect = %@", expected) ;
NSLog(@"sha1 test %@", [data isEqual:expected] ? @"passed" : @"failed") ;
}
#endif