-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSSYDigester.m
More file actions
139 lines (127 loc) · 4.77 KB
/
SSYDigester.m
File metadata and controls
139 lines (127 loc) · 4.77 KB
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
#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 ;
/* Using MD5 hash is deprecated in macOS 10.15. But it is still used
still used by Pinboard to generate a bookmark's exide from its URL,
and by Chrome to generate the checksum for its bookmarks file.
So, I ignore the deprecation*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CC_MD5_Init(&m_context_md5) ;
#pragma clang diagnostic pop
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:
m_algorithm = SSYDigesterAlgorithmMd5 ;
/* Using MD5 hash is deprecated in macOS 10.15. But it is still used
still used by Pinboard to generate a bookmark's exide from its URL,
and by Chrome to generate the checksum for its bookmarks file.
So, I ignore the deprecation*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CC_MD5_Update(&m_context_md5, [data bytes], (CC_LONG)[data length]) ;
#pragma clang diagnostic pop
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] ;
m_algorithm = SSYDigesterAlgorithmMd5 ;
/* Using MD5 hash is deprecated in macOS 10.15. But it is still used
still used by Pinboard to generate a bookmark's exide from its URL,
and by Chrome to generate the checksum for its bookmarks file.
So, I ignore the deprecation*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CC_MD5_Final([hash mutableBytes], &m_context_md5) ;
#pragma clang diagnostic pop
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