-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYDigester.m
116 lines (104 loc) · 3.48 KB
/
SSYDigester.m
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
#import "SSYDigester.h"
NSString* const msgSSYDigesterContextIsDefunct = @"Context is defunct." ;
@implementation SSYDigester
- (id)initWithAlgorithm:(SSYDigesterAlgorithm)algorithm {
self = [super init] ;
if (self) {
switch (algorithm) {
case SSYDigesterAlgorithmMd5:
m_algorithm = SSYDigesterAlgorithmMd5 ;
CC_MD5_Init(&m_context_md5) ;
break;
case SSYDigesterAlgorithmSha1:;
m_algorithm = SSYDigesterAlgorithmSha1 ;
CC_SHA1_Init(&m_context_sha1) ;
break;
case SSYDigesterAlgorithmSha256:;
m_algorithm = SSYDigesterAlgorithmSha256 ;
CC_SHA256_Init(&m_context_sha256) ;
break;
case SSYDigesterAlgorithmSha512:;
m_algorithm = SSYDigesterAlgorithmSha512 ;
CC_SHA512_Init(&m_context_sha512) ;
break;
}
}
return self ;
}
- (void)updateWithData:(NSData*)data {
switch (m_algorithm) {
case SSYDigesterAlgorithmMd5:
CC_MD5_Update(&m_context_md5, [data bytes], (CC_LONG)[data length]) ;
break ;
case SSYDigesterAlgorithmSha1:
CC_SHA1_Update(&m_context_sha1, [data bytes], (CC_LONG)[data length]) ;
break ;
case SSYDigesterAlgorithmSha256:
CC_SHA256_Update(&m_context_sha256, [data bytes], (CC_LONG)[data length]) ;
break ;
case SSYDigesterAlgorithmSha512:
CC_SHA512_Update(&m_context_sha512, [data bytes], (CC_LONG)[data length]) ;
break ;
}
}
- (void)updateWithString:(NSString*)string
encoding:(NSStringEncoding)encoding {
if (!string) {
return ;
}
NSInteger length ;
const char* encodedString = [string cStringUsingEncoding:encoding];
/* Note: encodedString is not necessarily a null-terminated C string. */
if (
(encoding == NSASCIIStringEncoding) ||
(encoding == NSUTF8StringEncoding)
) {
if (encodedString) {
length = (strlen(encodedString)) ;
}
else {
length = 0 ;
}
}
else if (
(encoding == NSUnicodeStringEncoding) || // aka NSUTF16StringEncoding
(encoding == NSUTF16BigEndianStringEncoding) ||
(encoding == NSUTF16LittleEndianStringEncoding)
) {
length = 2 * ([string length]) ;
}
else {
NSLog(@"Internal Error 214-4190") ;
length = 0 ;
}
NSData* data = [NSData dataWithBytes:encodedString
length:length] ;
[self updateWithData:data] ;
}
- (NSData*)finalizeDigest {
NSMutableData* hash ;
switch (m_algorithm) {
case SSYDigesterAlgorithmMd5:;
hash = [[NSMutableData alloc] initWithLength:CC_MD5_DIGEST_LENGTH] ;
CC_MD5_Final([hash mutableBytes], &m_context_md5) ;
break ;
case SSYDigesterAlgorithmSha1:;
hash = [[NSMutableData alloc] initWithLength:CC_SHA1_DIGEST_LENGTH] ;
CC_SHA1_Final([hash mutableBytes], &m_context_sha1) ;
break ;
case SSYDigesterAlgorithmSha256:;
hash = [[NSMutableData alloc] initWithLength:CC_SHA256_DIGEST_LENGTH] ;
CC_SHA256_Final([hash mutableBytes], &m_context_sha256) ;
break ;
case SSYDigesterAlgorithmSha512:;
hash = [[NSMutableData alloc] initWithLength:CC_SHA512_DIGEST_LENGTH] ;
CC_SHA512_Final([hash mutableBytes], &m_context_sha512) ;
break ;
}
NSData* answer = [NSData dataWithData:hash] ;
#if !__has_feature(objc_arc)
[hash release] ;
#endif
return answer ;
}
@end