Permalink
Browse files

Begin adding SHA256 sum to foundation categories.

  • Loading branch information...
1 parent 2d3bce7 commit cd5a7bfc52ceddc72aedaf658b8572956bca2d2a @calebd calebd committed Mar 5, 2013
@@ -29,6 +29,13 @@
*/
- (NSString *)SHA1Sum;
+/**
+ Returns a string of the SHA256 sum of the receiver.
+
+ @return The string of the SHA256 sum of the receiver.
+ */
+- (NSString *)SHA256Sum;
+
///-----------------------------------
/// @name Base64 Encoding and Decoding
@@ -41,6 +41,7 @@ - (NSString *)MD5Sum {
return [ms copy];
}
+
- (NSString *)SHA1Sum {
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
@@ -52,6 +53,17 @@ - (NSString *)SHA1Sum {
}
+- (NSString *)SHA256Sum {
+ NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
+ uint8_t digest[CC_SHA256_DIGEST_LENGTH];
+ CC_SHA256([self bytes], [self length], digest);
+ for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
+ [output appendFormat:@"%02x", digest[i]];
+ }
+ return output;
+}
+
+
// Adapted from http://www.cocoadev.com/index.pl?BaseSixtyFour
- (NSString *)base64EncodedString {
const uint8_t *input = self.bytes;
@@ -43,6 +43,13 @@
*/
- (NSString *)SHA1Sum;
+/**
+ Returns a string of the SHA256 sum of the receiver.
+
+ @return The string of the SHA256 sum of the receiver.
+ */
+- (NSString *)SHA256Sum;
+
///-------------------------
/// @name Comparing Versions
@@ -16,6 +16,7 @@ - (BOOL)containsString:(NSString *)string {
return !NSEqualRanges([self rangeOfString:string], NSMakeRange(NSNotFound, 0));
}
+
- (NSString *)MD5Sum {
const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:self.length];
@@ -30,6 +31,13 @@ - (NSString *)SHA1Sum {
}
+- (NSString *)SHA256Sum {
+ const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
+ NSData *data = [NSData dataWithBytes:cstr length:self.length];
+ return [data SHA256Sum];
+}
+
+
// Adapted from http://snipplr.com/view/2771/compare-two-version-strings
- (NSComparisonResult)compareToVersionString:(NSString *)version {
// Break version into fields (separated by '.')

0 comments on commit cd5a7bf

Please sign in to comment.