Hi,
How do i sha1 a string or set of numbers in Objective c?
Hi,
How do i sha1 a string or set of numbers in Objective c?
SHA1 doesn't actually come with Objective-C. You can use the C source code for hashdeep
and friends, which is licensed under the public domain (Because it was written by an employee of the United States government): http://md5deep.sourceforge.net/ .
CommonCrypto (an Apple framework) has functions for calculating SHA-1 hashes, including a one-step hash:
#include <CommonCrypto/CommonCrypto.h>
char digest[CC-SHA1_DIGEST_LENGTH];
NSData *stringBytes = [someString dataUsingEncoding: NSUTF8Encoding]; /* or some other encoding */
if (CC_SHA1([stringBytes bytes], [stringBytes length], digest)) {
/* SHA-1 hash has been calculated and stored in 'digest'. */
...
}
For a set of numbers, let us assume you mean an array of ints of known length. For such data, it is easier to iteratively construct the digest rather than use the one-shot function:
char digest[CC-SHA1_DIGEST_LENGTH];
uint32_t someIntegers[] = ...;
CC_SHA1_CTX ctx;
CC_SHA1_Init(&ctx);
{
for (size_t i = 0; i < (sizeof(someIntegers) / sizeof(*someIntegers)); i++)
CC_SHA1_Update(&ctx, someIntegers + i, sizeof(uint32_t));
}
CC_SHA1_Final(digest, &ctx);
/* SHA-1 hash has been calculated and stored in 'digest'. */
...