Question

I am using xcode and this is my sha512 method:

-(NSString*) sha512:(NSString*)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.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;
}

When I try to pass the input "test", it returns: "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff" which matches other sha512 hash tools (including my Java program and "http://hash.online-convert.com/sha512-generator").

However, when I input non-ascii char like "é", it returns something different than all my other sha512 tools. For input "é", my method returns: "60313f8521d3016916d876f7ad11cf42a30dfd4ff9bc557f1e2f90e0d37c56b76ab5e42c8a16db20c18086b0d769c08542429c262cc21ee4fba02bfc689a4797" when other tools (again including my Java program and "http://hash.online-convert.com/sha512-generator") return "9e2ad28633f24451bd4f3c1cb20586a21a44c3aeedbdc01b9cc8fa72917ea7bd689c82b8bf1fef89b911cf8cc46fa2c1ccc10087b2094fd4d3350ecd88526a2c".

Did I miss anything? Any ideas about this? Thanks!

Was it helpful?

Solution

Create your NSData object like this:

NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];

Just double checked and it works correctly like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top