標籤:
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.com11 //人為的增加密碼的難度,可以對 MD5 進行加鹽12 //使用者密碼 + 鹽值 MD5運算13 NSString *password = @"312816";14 //增加鹽值 ,且越長(複雜)越好15 NSString *salt = @"[email protected]#$%^&*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 //定義 hmacKey38 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 }
MD5加密運算