hash a password string using SHA512 like C#

前端 未结 2 1938
后悔当初
后悔当初 2020-12-13 06:55

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 shoul

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 07:54

    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 
    

提交回复
热议问题