Created
February 15, 2017 12:21
-
-
Save leo493852107/458bd0ea5f5d147696075091ef0a8a41 to your computer and use it in GitHub Desktop.
iOS MD5
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
// | |
// NSString+Hash.h | |
// | |
// Created by Tom Corwine on 5/30/12.. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSString (Hash) | |
@property (readonly) NSString *md5String; | |
@property (readonly) NSString *sha1String; | |
@property (readonly) NSString *sha256String; | |
@property (readonly) NSString *sha512String; | |
- (NSString *)hmacSHA1StringWithKey:(NSString *)key; | |
- (NSString *)hmacSHA256StringWithKey:(NSString *)key; | |
- (NSString *)hmacSHA512StringWithKey:(NSString *)key; | |
@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
// | |
// NSString+Hash.m | |
// | |
// Created by Tom Corwine on 5/30/12. | |
// | |
#import "NSString+hash.h" | |
#import <CommonCrypto/CommonDigest.h> | |
#import <CommonCrypto/CommonHMAC.h> | |
@implementation NSString (Hash) | |
- (NSString *)md5String | |
{ | |
const char *string = self.UTF8String; | |
int length = (int)strlen(string); | |
unsigned char bytes[CC_MD5_DIGEST_LENGTH]; | |
CC_MD5(string, length, bytes); | |
return [self stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH]; | |
} | |
- (NSString *)sha1String | |
{ | |
const char *string = self.UTF8String; | |
int length = (int)strlen(string); | |
unsigned char bytes[CC_SHA1_DIGEST_LENGTH]; | |
CC_SHA1(string, length, bytes); | |
return [self stringFromBytes:bytes length:CC_SHA1_DIGEST_LENGTH]; | |
} | |
- (NSString *)sha256String | |
{ | |
const char *string = self.UTF8String; | |
int length = (int)strlen(string); | |
unsigned char bytes[CC_SHA256_DIGEST_LENGTH]; | |
CC_SHA256(string, length, bytes); | |
return [self stringFromBytes:bytes length:CC_SHA256_DIGEST_LENGTH]; | |
} | |
- (NSString *)sha512String | |
{ | |
const char *string = self.UTF8String; | |
int length = (int)strlen(string); | |
unsigned char bytes[CC_SHA512_DIGEST_LENGTH]; | |
CC_SHA512(string, length, bytes); | |
return [self stringFromBytes:bytes length:CC_SHA512_DIGEST_LENGTH]; | |
} | |
- (NSString *)hmacSHA1StringWithKey:(NSString *)key | |
{ | |
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; | |
NSData *messageData = [self dataUsingEncoding:NSUTF8StringEncoding]; | |
NSMutableData *mutableData = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH]; | |
CCHmac(kCCHmacAlgSHA1, keyData.bytes, keyData.length, messageData.bytes, messageData.length, mutableData.mutableBytes); | |
return [self stringFromBytes:(unsigned char *)mutableData.bytes length:mutableData.length]; | |
} | |
- (NSString *)hmacSHA256StringWithKey:(NSString *)key | |
{ | |
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; | |
NSData *messageData = [self dataUsingEncoding:NSUTF8StringEncoding]; | |
NSMutableData *mutableData = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; | |
CCHmac(kCCHmacAlgSHA256, keyData.bytes, keyData.length, messageData.bytes, messageData.length, mutableData.mutableBytes); | |
return [self stringFromBytes:(unsigned char *)mutableData.bytes length:mutableData.length]; | |
} | |
- (NSString *)hmacSHA512StringWithKey:(NSString *)key | |
{ | |
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; | |
NSData *messageData = [self dataUsingEncoding:NSUTF8StringEncoding]; | |
NSMutableData *mutableData = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH]; | |
CCHmac(kCCHmacAlgSHA512, keyData.bytes, keyData.length, messageData.bytes, messageData.length, mutableData.mutableBytes); | |
return [self stringFromBytes:(unsigned char *)mutableData.bytes length:mutableData.length]; | |
} | |
#pragma mark - Helpers | |
- (NSString *)stringFromBytes:(unsigned char *)bytes length:(int)length | |
{ | |
NSMutableString *mutableString = @"".mutableCopy; | |
for (int i = 0; i < length; i++) | |
[mutableString appendFormat:@"%02x", bytes[i]]; | |
return [NSString stringWithString:mutableString]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment