Created
August 10, 2018 08:38
-
-
Save Viveron/36fe9eb7aa8347fbbf9b2968abf07705 to your computer and use it in GitHub Desktop.
NSData SHA256 hash category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSData (Hash) | |
- (NSData *)SHA256; | |
- (NSString *)stringSHA256; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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