NSString
;NSString
;NSString* string = @”Trololo”
. Consider the calculation of its hash sum by the MD5 algorithm. This is done very simply:
#import <CommonCrypto/CommonDigest.h> … NSString* string = @”Trololo”; const char* data = [string UTF8String]; unsigned char hashBuffer[CC_MD5_DIGEST_LENGTH]; CC_MD5(data, strlen(data), hashBuffer); NSData* result = [NSData dataWithBytes:hashBuffer length:CC_MD5_DIGEST_LENGTH];
CC_MD5(...)
and the constant CC_MD5_DIGEST_LENGTH
declared in the file we connected.
CCHmac(...)
, which as one of the parameters is passed the identifier of the hashing algorithm, in our case it is kCCHmacAlgMD5
. Do not forget only to connect in addition one more header file - <CommonCrypto/CommonHMAC.h>
.
#import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonHMAC.h> NSString* string = @”Trololo”; NSString* hmacKey = @”hmacKey”; const char* data = [string UTF8String]; const char* hmacData= [hmacKey UTF8String]; unsigned char hashBuffer[CC_MD5_DIGEST_LENGTH]; CCHmac(kCCHmacAlgMD5, hmacData, strlen(hmacData), data, strlen(data), hashBuffer); NSData* result = [NSData dataWithBytes:hashBuffer length:CC_MD5_DIGEST_LENGTH];
NSString* string = @”Trololo”; const char* data = [string UTF8String]; unsigned char hashBuffer[CC_MD5_DIGEST_LENGTH]; CC_MD5(data, strlen(data), hashBuffer); NSMutableString* result = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH*2]; for (int i= 0; i < CC_MD5_DIGEST_LENGTH; i++) [result appenFormat:@”%02X”, hashBuffer[i]];
NSData* buffer = [NSData dataWithBytes:hashBuffer length:CC_MD5_DIGEST_LENGTH]; NSString* result = [Base64 encode:buffer];
NSString
class. We create two files, respectively the header NSString+Hash.h
and the implementation file NSString+Hash.m
A category is declared almost as much as any class, with the following exceptions: after the class name, the category name is indicated in brackets, the class member declaration block is omitted. Thus, we obtain the following type of header file (all methods for working with the two most common hashing algorithms MD5 and SHA1 are laid down):
#import <Foundation/Foundation.h> #import <CommonCrypto/CommonHMAC.h> #import <CommonCrypto/CommonDigest.h> #import "Base64.h" @interface NSString (NSString_NM_HASH) //RAW - (NSData*) MD5; - (NSData*) SHA1; - (NSData*) HMAC_MD5: (NSString*)hmacKey; - (NSData*) HMAC_SHA1: (NSString*)hmacKey; //INTERPRET Base64 - (NSString*) MD5_x64; - (NSString*) SHA1_x64; - (NSString*) HMAC_MD5_x64:(NSString*)hmacKey; - (NSString*) HMAC_SHA1_x64:(NSString*)hmacKey; //INTERPRET HEX - (NSString*) MD5_HEX; - (NSString*) SHA1_HEX; - (NSString*) HMAC_MD5_HEX:(NSString*)hmacKey; - (NSString*) HMAC_SHA1_HEX:(NSString*)hmacKey; @end
#import “NSString+Hash.h” @implementation NSString (NSString_NM_HASH) - (NSData*) HMAC_SHA1: (NSString *)hmacKey{ const char* data = [self UTF8String]; const char* hashKey = [hmacKey UTF8String]; unsigned char hashingBuffer[CC_SHA1_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA1, hashKey, strlen(hashKey), data, strlen(data), hashingBuffer); return [NSData dataWithBytes:hashingBuffer length:CC_SHA1_DIGEST_LENGTH]; } - (NSString*) HMAC_SHA1_x64:(NSString *)hmacKey{ return [Base64 encode:[self HMAC_SHA1:hmacKey]]; } - (NSString*) HMAC_SHA1_HEX:(NSString *)hmacKey{ const char* data = [self UTF8String]; const char* hashKey = [hmacKey UTF8String]; unsigned char hashingBuffer[CC_SHA1_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA1, hashKey, strlen(hashKey), data, strlen(data), hashingBuffer); NSMutableString* result = [[NSMutableString alloc] initWithCapacity:CC_SHA1_DIGEST_LENGTH*2]; for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) [result appendFormat:@"%02X", hashingBuffer[i]]; return result; } @end
#import “NSString+Hash.h” NSString* string = @”Trololo”; NSString* string_md5 = [string MD5_HEX]; NSString* string_sh1 = [string SHA1_x64];
Source: https://habr.com/ru/post/133560/