Implementation of MD5/SHA1 encryption algorithm for IOS Objective-c

Source: Internet
Author: User
Tags base64 hash md5 md5 encryption sha1 sha1 encryption strlen

OBJECTIVE-C implementation of MD5 and SHA1 algorithm is relatively simple, you can directly call the system's C/s + + shared library to implement the call
MD5 is the message Digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete consistency of information transmission. is one of the most widely used hashing algorithms in computers
The SHA, Secure Hash algorithm (safe hashing algorithm) is a series of cryptographic hash functions issued by the National Institute of Standards and Technology (NIST), a design of the U.S. State Security Agency (NSA).


The use of the following methods:
MD5 Encryption method

  code is as follows copy code


 - (NSString *) MD5
{
    const char *CSTR = [self utf8string];
    unsigned char D Igest[cc_md5_digest_length];
    cc_md5 (CStr, strlen (CStr), Digest);
 
    nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
 
    for (int i = 0; i < cc_md5_digest_length; i++)
     &nb sp;  [Output appendformat:@ "%02x", Digest[i]];
 
    return output;
}

SHA1 Encryption method

The code is as follows Copy Code


-(nsstring*) SHA1
{
const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];
NSData *data = [NSData datawithbytes:cstr length:self.length];

uint8_t Digest[cc_sha1_digest_length];

CC_SHA1 (Data.bytes, 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;
}

Of course can also be combined with BASE64 to use, where the BASE64 encoding using GTMBASE64 implementation, need to import

The code is as follows Copy Code


-(NSString *) sha1_base64
{
const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];
NSData *data = [NSData datawithbytes:cstr length:self.length];

uint8_t Digest[cc_sha1_digest_length];

CC_SHA1 (Data.bytes, Data.length, Digest);

NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
Base64 = [GTMBase64 encodedata:base64];

NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];
return output;
}

-(NSString *) md5_base64
{
const char *CSTR = [self utf8string];
unsigned char digest[cc_md5_digest_length];
CC_MD5 (CStr, strlen (CStr), Digest);

NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
Base64 = [GTMBase64 encodedata:base64];

NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];
return output;
}

By expanding NSString to achieve full functionality, all code

The code is as follows Copy Code


@interface NSString (Encrypto)
-(NSString *) MD5;
-(NSString *) SHA1;
-(NSString *) sha1_base64;
-(NSString *) md5_base64;
-(NSString *) base64;

@end
@implementation NSString (Encrypto)
-(nsstring*) SHA1
{
const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];
NSData *data = [NSData datawithbytes:cstr length:self.length];

uint8_t Digest[cc_sha1_digest_length];

CC_SHA1 (Data.bytes, 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;
}

-(NSString *) MD5
{
const char *CSTR = [self utf8string];
unsigned char digest[cc_md5_digest_length];
CC_MD5 (CStr, strlen (CStr), Digest);

nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];

for (int i = 0; i < cc_md5_digest_length; i++)
[Output appendformat:@ "%02x", Digest[i]];

return output;
}

-(NSString *) sha1_base64
{
const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];
NSData *data = [NSData datawithbytes:cstr length:self.length];

uint8_t Digest[cc_sha1_digest_length];

CC_SHA1 (Data.bytes, Data.length, Digest);

NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
Base64 = [GTMBase64 encodedata:base64];

NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];
return output;
}

-(NSString *) md5_base64
{
const char *CSTR = [self utf8string];
unsigned char digest[cc_md5_digest_length];
CC_MD5 (CStr, strlen (CStr), Digest);

NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
Base64 = [GTMBase64 encodedata:base64];

NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];
return output;
}

-(NSString *) base64
{
NSData * data = [self datausingencoding:nsasciistringencoding allowlossyconversion:yes];
data = [GTMBase64 encodedata:data];
NSString * output = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
return output;
}
@end

Don't forget to import a CC-related library header file when implementing
Commoncrypto/commondigest.h

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.