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.
4 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>

- 111,848
- 21
- 189
- 228
-
ok.But I want to get CRC32 value of any data.Are there any way to get it in ios? – NSCry Feb 25 '12 at 15:50
-
-
Yeah, Apple is moving to transforms but I just tested this with the iOS 5.0 SDK. As for crc32, it is available in several libraries such as zlib. – zaph Feb 25 '12 at 17:16
-
Your SHA256 is working for me.I have another question I am generating hash from image data(NSData).Are there any calculator/website where I can check it? – NSCry Feb 26 '12 at 12:33
-
I checked it online sha256 generator the hash code is not matching.Any solutions? – NSCry Feb 29 '12 at 13:27
-
Notice that `CC_SHA256` computed the hash on data, not a string. If you are getting a different value you are providing different input in some manner. What site did you use? – zaph Feb 29 '12 at 13:42
-
I checked with http://www.xorbin.com/tools/sha256-hash-calculator and in my case I am generating sha256 from image nsdata.How shall I check my hash is correct ? this hash is not matching in web-server.Any suggestions – NSCry Feb 29 '12 at 13:55
-
How is the has sent? Does the server expect it to be Base64 encoded? Is the server using an HMAC function instead of just a MAC? Pasting binary data into a web page is probably not going to work. – zaph Feb 29 '12 at 14:54
-
CRC32 isn't a secure hash, like SHA-1, but it certainly is a hash function. – Glenn Maynard Nov 15 '12 at 21:51
-
Great Answer, thanks. However, please make sure that your code is ready for internationalization. All you have to do is change the encoding to NSUTF8StringEncoding. Then to calculate length you must to strlen(dataIn.bytes) – fzaziz Dec 12 '12 at 01:28
-
@fzaziz `dataIn.length` provides the length of the **data**, it makes no difference how the `NSData` was created. Also `strlen()` expects a null terminated string which is not guaranteed nor present in this example. I do agree that internationalization is important but is irrelevant to the question/answer. – zaph Dec 12 '12 at 10:59
-
@Zaph oops your point on strlen is right. I feel internationalization is very important especially in a question like this. Since no one will ask SHA256 for Chineese. The question is to get a hash for a string. Don't assume the string is in English, just changing the encoding to NSUTF8StringEncoding you can stop potential problems for a lot of people. – fzaziz Dec 13 '12 at 14:15
-
-
1To access the `CommonCrypto` library make sure you add the iOS `Security.framework` to your project. – Dimitris Dec 16 '17 at 15:59
For both of these, you can use this gist:
https://gist.github.com/paul-delange/6808278
And an example
NSString* crc32 = (__bridge NSString*)TGDFileHashCreateWithPath((__bridge CFStringRef)filepath, TGDFileHashDefaultChunkSizeForReadingData, TGDChecksumAlgorithmCRC32);

- 10,613
- 10
- 41
- 56
This method will generate crc32c as used by gcloud on iOS from a filepath. If you want the standard crc32 just uncomment the other value for CRC32_POLYNOMIAL.
It reads the file given in 512KB chunks so can be used on large files.
- (NSString*) crc32c:(NSString*)filepath{
/// using crc code from
// http://classroomm.com/objective-c/index.php?action=printpage;topic=2891.0
// by rgronlie
//this is the standard crc32 polynomial
//uint32_t CRC32_POLYNOMIAL = 0xEDB88320;
//this is the crc32c one
uint32_t CRC32_POLYNOMIAL = 0x82F63B78L;
uint32_t CRC32C_SEED = 0xFFFFFFFFL;
// create and populate a lookup table
uint32_t* pCRCTable = malloc(sizeof(uint32_t) * 256);
for (uint32_t i = 0; i <= 255; i++)
{
uint32_t crc32 = i;
for (uint32_t j = 8; j > 0; j--)
{
if ((crc32 & 1) == 1)
crc32 = (crc32 >> 1) ^ CRC32_POLYNOMIAL;
else
crc32 >>= 1;
}
pCRCTable[i] = crc32;
}
// get a handle to the file
NSFileHandle *filehandle = [NSFileHandle fileHandleForReadingAtPath:filepath];
if(filehandle == NULL){
NSLog(@"failed to create file handle");
return nil;
}
// a buffer to read into
NSData* databuffer;
uint32_t crc = CRC32C_SEED;
// read the file in chunks of 512KB
while(true){
databuffer = [filehandle readDataOfLength: 512 * 1024];
// if there is nothing left finish
if([databuffer length] == 0){
break;
}
// otherwise run each chunk through the lookup table
uint8_t *pBytes = (uint8_t *)[databuffer bytes];
uint32_t length = [databuffer length];
while (length--)
{
crc = (crc>>8) ^ pCRCTable[(crc & 0xFF) ^ *pBytes++];
}
}
// clean up
[filehandle closeFile];
free(pCRCTable);
// this is the result
uint32_t hash = crc ^ 0xFFFFFFFFL;
// reverse it for endianness
uint32_t hash_reversed = CFSwapInt32HostToBig(hash);
// as raw bytes
NSData* hash_data = [NSData dataWithBytes: &hash_reversed length: sizeof(hash_reversed)];
// return base64 encoded
return [hash_data base64EncodedStringWithOptions:0];
}

- 51
- 2
there are no apps which can generate Hash for ios
This should work....its for Mac

- 1
- 1
-
6Nice attempt to be helpful Yagnesh, but I think the original poster is asking for how to generate SHA256 & CRC32 numbers programmatically for his own program that does uploading. – Michael Dautermann Feb 25 '12 at 13:22
-
-
you [might get some helpful hints here in this related question/answer](http://stackoverflow.com/questions/1847281/commoncrypto-is-no-longer-part-of-the-iphone-sdk-where-else-can-i-easily-get-a), @ARC. – Michael Dautermann Feb 25 '12 at 16:10
-
1@MichaelDautermann I implemented CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes); and getting the hash sha256 data but it is not matching with online hash generator any suggestions – NSCry Feb 29 '12 at 13:30