我想从字符串创建哈希。如果字符串包含带重音的字符(如é,ő),则它会与NSASCIIStringEncoding崩溃。它不会与NSUTF8StringEncoding崩溃,但在服务器上它不匹配。
+(NSString *) getSHA256FromString: (NSString *)clear {
const char *s=[clear cStringUsingEncoding: NSASCIIStringEncoding];// NSUTF8StringEncoding works, but isn't the same on server
NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
CC_SHA256(keyData.bytes, keyData.length, digest);
NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
NSString *hash=[out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
return hash;
}