Skip to content

Instantly share code, notes, and snippets.

@Viveron
Created August 10, 2018 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viveron/36fe9eb7aa8347fbbf9b2968abf07705 to your computer and use it in GitHub Desktop.
Save Viveron/36fe9eb7aa8347fbbf9b2968abf07705 to your computer and use it in GitHub Desktop.
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