Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Generate SHA256 and CRC32 in ios

Tags:

ios

iphone

sha

I am doing a file uploading job. I want to generate SHA256 and CRC32 hashes. Can anyone help me how shall I generate those hash? I want to get it working for iOS.

like image 387
NSCry Avatar asked Feb 25 '12 12:02

NSCry


1 Answers

SHA256 is available in CommonCrypto. CRC32 is not a hash, it a Cyclic Redundancy Check.

Example code:

#import <CommonCrypto/CommonDigest.h>

NSData *dataIn = [@"Now is the time for all good computers to come to the aid of their masters." dataUsingEncoding:NSASCIIStringEncoding];
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];

CC_SHA256(dataIn.bytes, dataIn.length,  macOut.mutableBytes);

NSLog(@"dataIn: %@", dataIn);
NSLog(@"macOut: %@", macOut);

NSLog output:
dataIn: <4e6f7720 69732074 68652074 696d6520 666f7220 616c6c20 676f6f64 20636f6d 70757465 72732074 6f20636f 6d652074 6f207468 65206169 64206f66 20746865 6972206d 61737465 72732e>

macOut: <53f89cf6 7ebfbe56 89f1f76a 3843dfd1 09d68c5b a938dcd2 9a12004e 108260cb>

like image 92
zaph Avatar answered Sep 28 '22 18:09

zaph