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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值