IOS miscellaneous pen-9 (MD5 encryption), ios-9

Source: Internet
Author: User

IOS miscellaneous pen-9 (MD5 encryption), ios-9

First, an introduction to MD5

* From a big bull *

Message Digest Algorithm MD5 (the fifth version of the Message Digest Algorithm in Chinese) is a hash function widely used in the computer security field to provide Message integrity protection.

It is one of the widely used Hash Algorithms in computers (also translated digest algorithms and hash algorithms). mainstream programming languages generally have MD5 implementations.

The MD5 algorithm has the following features:

1. Compression: Data of any length, the calculated MD5 value length is fixed.

2. Easy Calculation: it is easy to calculate the MD5 value from the original data.

3. Anti-modification: Any modification to the original data, even if only one byte is modified, the MD5 value obtained is very different.

4. Strong anti-collision: it is very difficult to find a data with the same MD5 value (that is, forged data) because the original data and its MD5 value are known.

The function of MD5 is to compress large-capacity information into a confidential format before signing a private key using digital signature software (that is, to convert a byte string of any length into a certain length of sixteen number string ). In addition to MD5, sha-1, RIPEMD, and Haval are well-known.

/// ViewController. m // CX-MD5 encrypted /// Created by xubaoaichiyu on 16/3/22. // Copyright©Xubaoaichiyu in 2016. all rights reserved. // # import "ViewController. h "# import" NSString + Hash. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString * password = @ "xubaoaichiyu"; NSLog (@ "% @", password. md5String); // md5 salt NSString * repassword = [password stringByAppendingString: @ "love"]; NSLog (@ "% @", repassword. md5String) ;}@ end

MD5 can be said to be programmed in C language, but we will certainly implement it in three ways if we like object-oriented programming. Through this third-party door, we can easily solve the heavy C language code. The following is a third-party. h. m file.

////  NSString+Hash.h////  Created by Tom Corwine on 5/30/12..//#import <Foundation/Foundation.h>@interface NSString (Hash)@property (readonly) NSString *md5String;@property (readonly) NSString *sha1String;@property (readonly) NSString *sha256String;@property (readonly) NSString *sha512String;- (NSString *)hmacSHA1StringWithKey:(NSString *)key;- (NSString *)hmacSHA256StringWithKey:(NSString *)key;- (NSString *)hmacSHA512StringWithKey:(NSString *)key;@end
////  NSString+Hash.m////  Created by Tom Corwine on 5/30/12.//#import "NSString+hash.h"#import <CommonCrypto/CommonDigest.h>#import <CommonCrypto/CommonHMAC.h>@implementation NSString (Hash)- (NSString *)md5String{    const char *string = self.UTF8String;    int length = (int)strlen(string);    unsigned char bytes[CC_MD5_DIGEST_LENGTH];    CC_MD5(string, length, bytes);    return [self stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH];}- (NSString *)sha1String{    const char *string = self.UTF8String;    int length = (int)strlen(string);    unsigned char bytes[CC_SHA1_DIGEST_LENGTH];    CC_SHA1(string, length, bytes);    return [self stringFromBytes:bytes length:CC_SHA1_DIGEST_LENGTH];}- (NSString *)sha256String{    const char *string = self.UTF8String;    int length = (int)strlen(string);    unsigned char bytes[CC_SHA256_DIGEST_LENGTH];    CC_SHA256(string, length, bytes);    return [self stringFromBytes:bytes length:CC_SHA256_DIGEST_LENGTH];}- (NSString *)sha512String{    const char *string = self.UTF8String;    int length = (int)strlen(string);    unsigned char bytes[CC_SHA512_DIGEST_LENGTH];    CC_SHA512(string, length, bytes);    return [self stringFromBytes:bytes length:CC_SHA512_DIGEST_LENGTH];}- (NSString *)hmacSHA1StringWithKey:(NSString *)key{    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];    NSData *messageData = [self dataUsingEncoding:NSUTF8StringEncoding];    NSMutableData *mutableData = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];    CCHmac(kCCHmacAlgSHA1, keyData.bytes, keyData.length, messageData.bytes, messageData.length, mutableData.mutableBytes);    return [self stringFromBytes:(unsigned char *)mutableData.bytes length:mutableData.length];}- (NSString *)hmacSHA256StringWithKey:(NSString *)key{    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];    NSData *messageData = [self dataUsingEncoding:NSUTF8StringEncoding];    NSMutableData *mutableData = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];    CCHmac(kCCHmacAlgSHA256, keyData.bytes, keyData.length, messageData.bytes, messageData.length, mutableData.mutableBytes);    return [self stringFromBytes:(unsigned char *)mutableData.bytes length:mutableData.length];}- (NSString *)hmacSHA512StringWithKey:(NSString *)key{    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];    NSData *messageData = [self dataUsingEncoding:NSUTF8StringEncoding];    NSMutableData *mutableData = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];    CCHmac(kCCHmacAlgSHA512, keyData.bytes, keyData.length, messageData.bytes, messageData.length, mutableData.mutableBytes);    return [self stringFromBytes:(unsigned char *)mutableData.bytes length:mutableData.length];}#pragma mark - Helpers- (NSString *)stringFromBytes:(unsigned char *)bytes length:(int)length{    NSMutableString *mutableString = @"".mutableCopy;    for (int i = 0; i < length; i++)        [mutableString appendFormat:@"%02x", bytes[i]];    return [NSString stringWithString:mutableString];}@end

 

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.