views:

91

answers:

3
+1  Q: 

Objective C: SHA1

Hi,

How do i sha1 a string or set of numbers in Objective c?

+1  A: 

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

Billy ONeal
Another option would be libgcrypt, from the makers of GnuPG (http://www.gnupg.org/related_software/libraries.en.html#lib-libgcrypt).
schot
Is there any secure encryption that is supported directly for obj c and php?
Daniel
@schot: But libcrypt has a much more restrictive license. @Daniel: Not out of the box, no. Objective-C does not come with much in the way of libraries.
Billy ONeal
A: 

You can use the openssl library. Or you can use/write some implementation. Check for a sample reference here.

Praveen S
+2  A: 

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'. */
...
Jonathan Grynspan
Common Crypto is not apart of the SDK any more
Daniel
As of 4.0.2, yes it is. Go ahead and try it!
Jonathan Grynspan