0

Im trying to make same Sha1 out put in IOS but the result aren't the same.

Due to this answer SO it should be fine and same result

this is my C# Code :

    using (SHA1 sha1Hash = SHA1.Create())
     {
            string hash1 = GetSHA1Hash(sha1Hash, "8390495498");
     }

    static string GetSHA1Hash(SHA1 sha1Hash, string input)
    { 
        byte[] data = sha1_Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }

the result is : d763bd0b5b346a3c95eb4f2912be40c2ce6f0a41

And my Objective C code :

   [sh1obj sha1:@"8390495498"]
- (NSString *)sha1:(NSString *)str {
    NSData *data = [str dataUsingEncoding:NSUnicodeStringEncoding];

    NSString *unicodePassword = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];
    data = [unicodePassword dataUsingEncoding:NSUnicodeStringEncoding];
    data = [NSData dataWithBytes:[data bytes] + 2 length:[data length] - 2];

    unsigned char hash[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1([data bytes], [data length], hash);
    NSData *result = [NSData dataWithBytes:hash length:CC_SHA1_DIGEST_LENGTH];
    NSLog(@"Result: %@",result);

    return result;
}

the result is : <241cdda1 6867aaad cb69faed d0975579 f6ec3a07>

i even tried this one too :

  [sh1obj sha1:@"8390495498"]
- (NSString *)sha1:(NSString *)str {

    const char *cStr = [str UTF8String];
    unsigned char result[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(cStr, strlen(cStr), result);
    NSString *s = [NSString  stringWithFormat:
                   @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                   result[0], result[1], result[2], result[3], result[4],
                   result[5], result[6], result[7],
                   result[8], result[9], result[10], result[11], result[12],
                   result[13], result[14], result[15],
                   result[16], result[17], result[18], result[19]
                   ];
    NSLog(@"Result: %@",s);
    return s;
}

and the result was : cbdb5d7936ac9a56846cabad5e9d41942a08b49c

Community
  • 1
  • 1
Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
  • 1
    What does the +2 do in dataWithBytes? And have you tried NSUTF8StringEncoding? – Thilo Oct 23 '14 at 07:36
  • 1
    And why do you put the NSData back into an NSString and then decode it again? – Thilo Oct 23 '14 at 07:37
  • I'm trying to remove BOM with "+2". after i changed to NSUTF8StringEncoding it gives me the result of third method. if i remove the "+2" method it gives me another sha1 : 0e225a4e 22393d01 a85a44b4 8d5f3ed3 72515a7d at this moment don't know what to do :( – Ahad Porkar Oct 23 '14 at 07:42
  • oh that was a typo , i fixed it. – Ahad Porkar Oct 23 '14 at 07:56

1 Answers1

1

I think the Objective-C code is correct:

$ echo -n 8390495498 | openssl sha1  
cbdb5d7936ac9a56846cabad5e9d41942a08b49c
Thilo
  • 257,207
  • 101
  • 511
  • 656