Skip to content

Commit 337912d

Browse files
committedJan 8, 2018
SSYDigester now supports additional algorithms, and ARC or not.
1 parent 79f626a commit 337912d

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed
 

‎SSYDigester.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33

44
enum SSYDigesterAlgorithm_enum {
55
SSYDigesterAlgorithmMd5,
6-
SSYDigesterAlgorithmSha1
6+
SSYDigesterAlgorithmSha1,
7+
SSYDigesterAlgorithmSha256,
8+
SSYDigesterAlgorithmSha512,
79
} ;
810
typedef enum SSYDigesterAlgorithm_enum SSYDigesterAlgorithm ;
911

1012
/*!
1113
@brief A class for computing message digests incrementally.
12-
13-
@details
1414
*/
1515
@interface SSYDigester : NSObject {
1616
SSYDigesterAlgorithm m_algorithm ;
1717
CC_MD5_CTX m_context_md5 ;
1818
CC_SHA1_CTX m_context_sha1 ;
19+
CC_SHA256_CTX m_context_sha256 ;
20+
CC_SHA512_CTX m_context_sha512 ;
1921
}
2022

2123
/*!
@@ -147,4 +149,4 @@ typedef enum SSYDigesterAlgorithm_enum SSYDigesterAlgorithm ;
147149
NSLog(@"sha1 test %@", [data isEqual:expected] ? @"passed" : @"failed") ;
148150
}
149151

150-
#endif
152+
#endif

‎SSYDigester.m

+32-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ - (id)initWithAlgorithm:(SSYDigesterAlgorithm)algorithm {
1616
m_algorithm = SSYDigesterAlgorithmSha1 ;
1717
CC_SHA1_Init(&m_context_sha1) ;
1818
break;
19+
case SSYDigesterAlgorithmSha256:;
20+
m_algorithm = SSYDigesterAlgorithmSha256 ;
21+
CC_SHA256_Init(&m_context_sha256) ;
22+
break;
23+
case SSYDigesterAlgorithmSha512:;
24+
m_algorithm = SSYDigesterAlgorithmSha512 ;
25+
CC_SHA512_Init(&m_context_sha512) ;
26+
break;
1927
}
2028
}
2129

@@ -30,6 +38,12 @@ - (void)updateWithData:(NSData*)data {
3038
case SSYDigesterAlgorithmSha1:
3139
CC_SHA1_Update(&m_context_sha1, [data bytes], (CC_LONG)[data length]) ;
3240
break ;
41+
case SSYDigesterAlgorithmSha256:
42+
CC_SHA256_Update(&m_context_sha256, [data bytes], (CC_LONG)[data length]) ;
43+
break ;
44+
case SSYDigesterAlgorithmSha512:
45+
CC_SHA512_Update(&m_context_sha512, [data bytes], (CC_LONG)[data length]) ;
46+
break ;
3347
}
3448
}
3549

@@ -40,14 +54,15 @@ - (void)updateWithString:(NSString*)string
4054
return ;
4155
}
4256

43-
NSInteger length ;
44-
const char* cString = [string cStringUsingEncoding:encoding] ;
57+
NSInteger length ;
58+
const char* encodedString = [string cStringUsingEncoding:encoding];
59+
/* Note: encodedString is not necessarily a null-terminated C string. */
4560
if (
4661
(encoding == NSASCIIStringEncoding) ||
4762
(encoding == NSUTF8StringEncoding)
4863
) {
49-
if (cString) {
50-
length = (strlen(cString)) ;
64+
if (encodedString) {
65+
length = (strlen(encodedString)) ;
5166
}
5267
else {
5368
length = 0 ;
@@ -65,7 +80,7 @@ - (void)updateWithString:(NSString*)string
6580
length = 0 ;
6681
}
6782

68-
NSData* data = [NSData dataWithBytes:cString
83+
NSData* data = [NSData dataWithBytes:encodedString
6984
length:length] ;
7085
[self updateWithData:data] ;
7186
}
@@ -81,10 +96,21 @@ - (NSData*)finalizeDigest {
8196
hash = [[NSMutableData alloc] initWithLength:CC_SHA1_DIGEST_LENGTH] ;
8297
CC_SHA1_Final([hash mutableBytes], &m_context_sha1) ;
8398
break ;
99+
case SSYDigesterAlgorithmSha256:;
100+
hash = [[NSMutableData alloc] initWithLength:CC_SHA256_DIGEST_LENGTH] ;
101+
CC_SHA256_Final([hash mutableBytes], &m_context_sha256) ;
102+
break ;
103+
case SSYDigesterAlgorithmSha512:;
104+
hash = [[NSMutableData alloc] initWithLength:CC_SHA512_DIGEST_LENGTH] ;
105+
CC_SHA512_Final([hash mutableBytes], &m_context_sha512) ;
106+
break ;
84107
}
85108
NSData* answer = [NSData dataWithData:hash] ;
109+
#if !__has_feature(objc_arc)
86110
[hash release] ;
111+
#endif
112+
87113
return answer ;
88114
}
89115

90-
@end
116+
@end

0 commit comments

Comments
 (0)
Please sign in to comment.