Error in sha1 encryption of oc ios Chinese strings ?,
I found an oc-version encryption tool on the Internet, but there is a big problem in encrypting Chinese.
Const char * cstr = [self cStringUsingEncoding: encoding];
NSData * data = [NSData dataWithBytes: cstr length: self. length];
Uint8_t digest [CC_SHA1_DIGEST_LENGTH];
CC_SHA1 (data. bytes, data. length, digest );
NSMutableString * result = [NSMutableString stringWithCapacity: CC_SHA1_DIGEST_LENGTH * 2];
For (int I = 0; I <CC_SHA1_DIGEST_LENGTH; I ++ ){
[Result appendFormat: @ "% 02x", digest [I];
}
Return result. uppercaseString;
The above method causes data loss when converting a Chinese string to data.
Const char * cstr = [input cStringUsingEncoding: NSUTF8StringEncoding];
NSData * data = [NSData dataWithBytes: cstr length: input. length];
Change these two sentences
NSData * data = [input dataUsingEncoding: NSUTF8StringEncoding];
You can.
Ps, this is because we encounter a string first converted to UTF-8, and the background encountered a direct sha1 encryption instead of utf8.