22

I am developing logon function for my iPhone Application, so I want to hash the password using the SHA512 hashing algorithm then get the result as NSString (the result should be the same with SHA512 in C#). After spending a lot of time in the internet, I still not find out the solution yet! :(

Is there anyone has the solution and sample code, please help me! Thanks a lot!

[Update] In my C# code, the password is stored using SecureString, so maybe it's cause make different byte array between objective-c and C#

Son Nguyen
  • 3,481
  • 4
  • 33
  • 47

2 Answers2

32

This function will hash a string using SHA512. The resulting string is a hex representation of the hash:

+ (NSString *) createSHA512:(NSString *)source {

    const char *s = [source cStringUsingEncoding:NSASCIIStringEncoding];

    NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};

    CC_SHA512(keyData.bytes, keyData.length, digest);

    NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];

    return [out description];
}

Don't forget to include the correct header:

#include <CommonCrypto/CommonDigest.h>
Joe
  • 46,419
  • 33
  • 155
  • 245
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • Dear Philippe, I have a problem that when comparing hashed passwords are different between C# and objective-c. In my C# code, I used SecureString to store password, so I have to use Marshal Copy to get byte array and I saw that there are 0bytes appended after each password's char, maybe that's cause the hashed pass are different. I don't know how to resolve it, could you please help me again? thanks! – Son Nguyen Oct 01 '10 at 04:20
  • I also get an extra 0 byte after each char... any solution for this? – BoldAsLove May 25 '15 at 00:23
  • nevermind, I was under the assumption that my conversion for my byte array to my hex string was correct on the java side..... I was not the one who wrote that and need to learn my lesson never to make assumptions. my hex conversion on the java side was wrong and left out that extra 0 after each character. Thanks for the working code @Leybaert – BoldAsLove May 25 '15 at 01:41
  • your answer just use `description` which is not as good as it should be. Descrption is just A string that contains a hexadecimal representation of the object’s contents in a property list format. So it if formatted, not the plain string. – Wingzero Nov 14 '15 at 04:22
18

I am using this one.

It matches PHP SHA512 algorithm output:

<?php `hash('sha512', 'The quick brown fox jumped over the lazy dog.');` ?>


Objective-C code:

+(NSString *)createSHA512:(NSString *)string
{
    const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:string.length];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, data.length, digest);
    NSMutableString* output = [NSMutableString  stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}
capikaw
  • 12,232
  • 2
  • 43
  • 46
Martynas
  • 181
  • 1
  • 2
  • 1
    +1 This is works better for me. The answer's output is undesirable for php comparison: <9870d986 f510384d 932cdff5 13fb1886 0eee6d6f 1e90e794 bd25b980 bd0a48d1 d5fdd937 f454408f e93f5a61 e8724993 0e153424 41a24f9f 3059a9ea 3e029f2a> Where as this answer outputs something like: 9870d986f510384d932cdff513fb18860eee6d6f1e90e794bd25b980bd0a48d1d5fdd937f454408fe93f5a61e87249930e15342441a24f9f3059a9ea3e029f2a – capikaw Jan 30 '13 at 02:41
  • 2
    why do you have to multiply for 2 in output: CC_SHA512_DIGEST_LENGTH * 2 ? – J.Williams Apr 29 '15 at 17:52
  • 1
    @SisterRay As the name implies, it's 512 bits, that is 64 bytes. But that's the hash, maybe you're wondering about a specific representation of that hash in string, as is commonly used, then it depends of the given representation. If you write the hash in hexa, then it will be 128 characters. If you write the hash in base64, then it will be 86 bytes (or 88 with padding). – Wingzero Nov 14 '15 at 04:14
  • I borrow it from http://stackoverflow.com/questions/18236106/what-is-the-length-of-a-hashed-string-with-sha512 – Wingzero Nov 14 '15 at 04:14