Error in sha1 encryption of oc ios Chinese strings ?,

Source: Internet
Author: User
Tags sha1 encryption

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.