IOS SHA1 Encryption algorithm

Source: Internet
Author: User
Tags sha1 sha1 encryption



Recently in the project to use the network Request signature authentication method, so on the internet to find theOC sha1加密method, quickly found a public use of the package good method, the following code is


First you need to add the header file
#import <CommonCrypto / CommonDigest.h>
Then use the following method directly
// sha1 encryption method
-(NSString *) sha1: (NSString *) input
{
     const char * cstr = [input cStringUsingEncoding: NSUTF8StringEncoding];
     NSData * data = [NSData dataWithBytes: cstr length: input.length];

     uint8_t digest [CC_SHA1_DIGEST_LENGTH];
     CC_SHA1 (data.bytes, (unsigned int) data.length, digest);
     NSMutableString * output = [NSMutableString stringWithCapacity: CC_SHA1_DIGEST_LENGTH * 2];

     for (int i = 0; i <CC_SHA1_DIGEST_LENGTH; i ++) {
         [output appendFormat: @ "% 02x", digest [i]];
     }
     return output;
}


I used this method directly in the project and solved the problem perfectly, but this is what I'm talking about today. however , in the later project modification, the需要加密的字符串Chinese characters are added (there is no Chinese characters in the string that need to be encrypted), In this case, the above method and the server side of the encryption不一样(difficult to debug the process of troubleshooting issues not to repeat);



The reason for the final discovery is that the encryption string generated on my side is服务器not the same as that generated there, and the error is naturally occurring because加密的字符串中包含有汉字
Then search online, in < topic: How to SHA1 encryption of Chinese strings > Find the method in this post, and now contribute to everyone


First you need to add the header file
#import <CommonCrypto / CommonDigest.h>
Then use the following method directly
// sha1 encryption method
-(NSString *) sha1: (NSString *) input
{
     // const char * cstr = [input cStringUsingEncoding: NSUTF8StringEncoding];
     // NSData * data = [NSData dataWithBytes: cstr length: input.length];

      NSData * data = [input dataUsingEncoding: NSUTF8StringEncoding];

     uint8_t digest [CC_SHA1_DIGEST_LENGTH];

     CC_SHA1 (data.bytes, (unsigned int) data.length, digest);

     NSMutableString * output = [NSMutableString stringWithCapacity: CC_SHA1_DIGEST_LENGTH * 2];

     for (int i = 0; i <CC_SHA1_DIGEST_LENGTH; i ++) {
         [output appendFormat: @ "% 02x", digest [i]];
     }

     return output;
}


We can see the difference between this method and the first one, the first two sentences are commented out, and the


NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];


Instead of the effect of the two sentences;



Posts on the first floor bindbasic The exact same thing.


Using the above method to convert Chinese strings to data will cause data loss.
Put
const char * cstr = [input cStringUsingEncoding: NSUTF8StringEncoding];
     NSData * data = [NSData dataWithBytes: cstr length: input.length];

These two sentences are changed to

NSData * data = [input dataUsingEncoding: NSUTF8StringEncoding];
Just fine


After the actual measurement, the encryption method of the second method is suitable for pure strings and strings with Chinese characters. It is recommended, recommended, recommended! ! ! (Important thing said three times)



Note: Strictly speaking,SHA1 (security [hashing algorithm]) is just called an algorithm, for Verify data integrity, and not called加密~






RELATED links:



IOS SHA1 encryption Method (hash algorithm, used to verify data integrity) and encryption methods for cases in which characters are contained in a string



Introduction to MD5 and SHA-1 encryption (with iOS encryption)



How HMAC_SHA1 is decrypted in iOS



Implementation of the OBJECTIVE-C MD5/SHA1 encryption algorithm for iOS development



IOS SHA1 Encryption algorithm


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.