您的位置:首页 > 其它

MD5加密运算

2015-11-19 12:08 429 查看
//MD5 对字符串的加密
-(void)demo1 {
NSString *str = @"love";

//对字符串进行MD5加密
str = str.md5String;

NSLog(@"str : %@",str);

//对于比较简单的密码,可以通过一些网站查到,如:http://www.cmd5.com
//人为的增加密码的难度,可以对 MD5 进行加盐
//用户密码 + 盐值   MD5运算
NSString *password = @"312816";
//增加盐值 ,且越长(复杂)越好
NSString *salt = @"zhufengshib!@#$%^&*sdfgh";
//给密码添加盐值
password = [password stringByAppendingString:salt];
//进行 MD5运算
password = password.md5String;

//加盐,是一种比较高级的加密算法
NSLog(@"password : %@",password);
}

//hmac 加密运算
-(void)demo2 {
//利用 block 定义一个字符串
__block NSString *password = @"hello";

//创建网络请求,并发送
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/login/hmackey.php"];

//发送网络请求
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//把 JSON数据 转换成 OC 数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//定义 hmacKey
NSString *hmacKey = dict[@"hmacKey"];
NSLog(@"hmacKey : %@",hmacKey);

//使用从服务器获取的 hmacKey 对密码进行 hmac 运算
password = [password hmacMD5StringWithKey:hmacKey];

NSLog(@"password : %@",password);

}] resume];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: