MD5加密运算

 1 //MD5 对字符串的加密
 2 -(void)demo1 {
 3     NSString *str = @"love";
 4     
 5     //对字符串进行MD5加密
 6     str = str.md5String;
 7     
 8     NSLog(@"str : %@",str);
 9     
10     //对于比较简单的密码,可以通过一些网站查到,如:http://www.cmd5.com
11     //人为的增加密码的难度,可以对 MD5 进行加盐
12     //用户密码 + 盐值   MD5运算
13     NSString *password = @"312816";
14     //增加盐值 ,且越长(复杂)越好
15     NSString *salt = @"zhufengshib!@#$%^&*sdfgh";
16     //给密码添加盐值
17     password = [password stringByAppendingString:salt];
18     //进行 MD5运算
19     password = password.md5String;
20     
21     //加盐,是一种比较高级的加密算法
22     NSLog(@"password : %@",password);
23 }
24 
25 //hmac 加密运算
26 -(void)demo2 {
27     //利用 block 定义一个字符串
28     __block NSString *password = @"hello";
29     
30     //创建网络请求,并发送
31     NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/login/hmackey.php"];
32     
33     //发送网络请求
34     [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
35         //把 JSON数据 转换成 OC 数据
36         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
37         //定义 hmacKey
38         NSString *hmacKey = dict[@"hmacKey"];
39         NSLog(@"hmacKey : %@",hmacKey);
40         
41         //使用从服务器获取的 hmacKey 对密码进行 hmac 运算
42         password = [password hmacMD5StringWithKey:hmacKey];
43         
44         NSLog(@"password : %@",password);
45         
46     }] resume];
47 }
原文地址:https://www.cnblogs.com/zhufengshibei/p/4977206.html