.net vs Objective c SHA-512 mismatch
Asked Answered
P

3

6

I am trying to write function for creating sha512 string in objective from .net function which is

public static string GetSHA512(string strPlain)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] HashValue = null;
    byte[] MessageBytes = UE.GetBytes(strPlain);
    System.Security.Cryptography.SHA512Managed SHhash = new System.Security.Cryptography.SHA512Managed();
    string strHex = string.Empty;

    HashValue = SHhash.ComputeHash(MessageBytes);
    foreach (byte b in HashValue)
    {
        strHex += String.Format("{0:x2}", b);
    }
    return strHex;
}

This gives result as

input : pass123
output: 2a6353744cc2914c602265f50d2e413d0561368775756392517abb340ef75d52ee0c5d3623ddd1826fd768a13dca8961f5957c75df0d793b9d7537aabe050705

What I have tried is as follow

-(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];

    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}

which gives result below

input : pass123
output: fd37ca5ca8763ae077a5e9740212319591603c42a08a60dcc91d12e7e457b024f6bdfdc10cdc1383e1602ff2092b4bc1bb8cac9306a9965eb352435f5dfe8bb0

Can anyone please suggest what am I doing wrong ?

Why these two values differ ?

Please correct my mistakes.

EDIT

Mean While I have tried changing encoding to NSUTF16StringEncoding and NSUnicodeStringEncoding which results still different and as follows

input : pass123
output: 514331e3f7ca0a295539347ebccc4e4f095fe5f3c1df10d43b4d550144c7b30ba9507831893ea63ea22e62e993be529b0d14be7800a90aa0de199d6be62a5f1b
Pinnatipartite answered 1/10, 2013 at 7:27 Comment(0)
P
4

Little bit of trial and error in function and searching for compitable version of encoding found here help me to resolve problem.

In function conversion to const char was not necessary so removed and directly converted string to data using default method available.

-(NSString *)createSHA512:(NSString *)string {
    NSData *data = [string dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, data.length, digest);
    NSMutableString* output = [NSMutableString  stringWithCapacity:CC_SHA512_DIGEST_LENGTH];

    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}
Pinnatipartite answered 3/10, 2013 at 5:21 Comment(0)
G
5

In the Objective C version you're converting the text to binary using UTF-8. In the .NET version you're using UTF-16. That may not be the only difference, but it's certainly a relevant one.

I'd rewrite your .NET method as:

public static string GetSHA512(string text)
{
    byte[] messageBytes = Encoding.UTF8.GetBytes(text);

    byte[] hash;
    using (SHA512 hashAlgorithm = SHA512.Create())
    {
        hash = hashAlgorithm.ComputeHash(messageBytes);
    }

    StringBuilder builder = new StringBuilder();
    foreach (byte b in hash)
    {
        builder.AppendFormat("{0:x2}", b);
    }        
    return builder.ToString();
}
Gabar answered 1/10, 2013 at 7:28 Comment(4)
Thank you for your quick response but we are in a stage where we cannot change .net function and need to write compitable function in objective c.Pinnatipartite
@JanakNirmal So simply do: const char *cstr = [string cStringUsingEncoding:NSUTF16StringEncoding];Heterogony
@JanakNirmal: It would have been worth saying that to start with. In that case you need to change your Objective C code to use NSUTF16StringEncoding.Gabar
I have already tried mean while when I responded to answer but the result is still different. Updated question accordingly. Thax for your input though.Pinnatipartite
P
4

Little bit of trial and error in function and searching for compitable version of encoding found here help me to resolve problem.

In function conversion to const char was not necessary so removed and directly converted string to data using default method available.

-(NSString *)createSHA512:(NSString *)string {
    NSData *data = [string dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, data.length, digest);
    NSMutableString* output = [NSMutableString  stringWithCapacity:CC_SHA512_DIGEST_LENGTH];

    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}
Pinnatipartite answered 3/10, 2013 at 5:21 Comment(0)
C
0

For base64encoded string:

-(NSString *)createSHA512Net:(NSString *)string {
    NSData *data = [string dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, data.length, digest);
    NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
    NSString *str = [out  base64EncodedStringWithOptions:kNilOptions]; 
    return str;
}
Coax answered 4/2, 2016 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.