SHA1 hash producing different result in Objective-C and C#.NET

前端 未结 2 1183
醉梦人生
醉梦人生 2021-02-11 07:56

Basically I want to write function that computes sha1 hash.

So far I have tried is as follows.

C#.NET

byte[] p2 = System.Text.En         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-11 08:49

    This line is wrong, since you interpret UTF8 encoded data as if it was Unicode encoded:

    NSString *unicodePassword = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];

    Replce the second line with: NSData *data = [password dataUsingEncoding:NSUnicodeStringEncoding];

    The 2 first bytes in data is a BOM (Byte order mark).

    Remove these 2 bytes with data = [NSData dataWithBytes:[data bytes] + 2 length:[data length] - 2];

    ...and then hash that data any you will have the same hash as in C#.

提交回复
热议问题