Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <CommonCrypto/CommonHMAC.h>
  2.  
  3.     NSString *hashkey = @"Some text to hash";
  4.     // PHP uses ASCII encoding, not UTF
  5.     const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding];
  6.     NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
  7.    
  8.     // This is the destination
  9.     uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
  10.     // This one function does an unkeyed SHA1 hash of your hash data
  11.     CC_SHA1(keyData.bytes, keyData.length, digest);
  12.    
  13.     // Now convert to NSData structure to make it usable again
  14.     NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
  15.     // description converts to hex but puts <> around it and spaces every 4 bytes
  16.     NSString *hash = [out description];
  17.     hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
  18.     hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
  19.     hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
  20.    
  21.     NSLog(@"hash: %@", hash);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement