MD5 encryption in iOS development practices
What is MD5:
Message Digest Algorithm 5 (Message Digest Algorithm version 5th"
Effect: generate a unique 128-bit hash value for the input information (32 characters)
MD5 features:
Two different plaintext values are input, and the same output value is not obtained.
According to the output value, the original plain text cannot be obtained, that is, the process is irreversible.
MD5 application:
The MD5 encryption algorithm is widely used because it is secure and free of charge.
It is mainly used in digital signatures, file integrity verification, and password encryption.
MD5 improvements:
Add Salt (Salt): Insert a random string at a fixed position in the plain text, and then perform MD5
Encryption first, followed by out-of-order: MD5 is performed on the plaintext, and then the characters in the encrypted MD5 string are in out-of-order.
Chestnuts:
Import NSString + hash. h encapsulated encryption classification algorithm: http://pan.baidu.com/s/1o7hA4Vw
ViewController. m
// ViewController. m # import "ViewController. h "# import" NSString + Hash. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self MD5]; [self MD5Salt: @" san "]; [self doubleMD5]; [self MD5Reorder];} // MD5 encryption-(void) MD5 {NSString * pwd = @ "123456"; NSString * pwdMD5 = [pwd md5String]; NSLog (@ "% @", pwdMD5);}/*** salt encryption MD5 ($ pass. $ salt) */-(void) MD5Salt :( NSString *) salt {NSString * pwd = @ "123456"; pwd = [pwd stringByAppendingString: salt]; // salt: randomly insert any string NSString * pwdMD5 = [pwd md5String]; NSLog (@ "% @", pwdMD5 );} /*** MD5 (MD5 ($ pass) */-(void) doubleMD5 {NSString * pwd = @ "123456 "; NSString * pwdMD5MD5 = [[pwd md5String] md5String]; NSLog (@ "% @", pwdMD5MD5);}/*** encrypt first, then unordered */-(void) MD5Reorder {NSString * pwd = @ "123456"; NSString * pwdMD5 = [pwd md5String]; NSLog (@ "oldpwdMD5 = % @", pwdMD5 ); NSString * prefix = [pwdMD5 substringFromIndex: 3]; // capture (including 3) NSString * subfix = [pwdMD5 substringToIndex: 3] starting from the subscript 3; // truncate a string from 0 to 3 (excluding 3) pwdMD5 = [prefix stringByAppendingString: subfix]; NSLog (@ "newpwdMD5 = % @", pwdMD5);} @ end