Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSData SHA256 hash category
#import <Foundation/Foundation.h>
@interface NSData (Hash)
- (NSData *)SHA256;
- (NSString *)stringSHA256;
@end
#import "NSData+Hash.h"
#import <CommonCrypto/CommonCrypto.h>
#import "NSString+Hash.h"
@implementation NSData (Hash)
- (NSData *)SHA256 {
unsigned char buffer[CC_SHA256_DIGEST_LENGTH];
NSData *hashedData = nil;
if (CC_SHA256(self.bytes, (CC_LONG)self.length, buffer)) {
hashedData = [NSData dataWithBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
}
return hashedData;
}
- (NSString *)stringSHA256 {
unsigned char buffer[CC_SHA256_DIGEST_LENGTH];
NSString *hashedString = nil;
if (CC_SHA256(self.bytes, (CC_LONG)self.length, buffer)) {
hashedString = [NSString stringWithHash:buffer digestLength:CC_SHA256_DIGEST_LENGTH];
}
return hashedString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment