IOS开发之MD5加密和钥匙串的使用-oc
生活随笔
收集整理的這篇文章主要介紹了
IOS开发之MD5加密和钥匙串的使用-oc
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發之MD5加密和鑰匙串的使用-oc
源碼在我的主頁,md5加密是用戶登錄安全的一個保障。不可逆的,可以暴力破解的。
// // ViewController.m // MD5演練 // // Created by 魯軍 on 2021/3/18. //#import "ViewController.h" #import "NSString+Hash.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {[super viewDidLoad]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self test1]; // [self addSalt];[self diffcult]; }-(NSString *)getPWD:(NSString *)pwd{NSString *md5Key = [@"LuJun" md5String];NSString *hmacKey = [pwd hmacSHA256StringWithKey:md5Key];NSDate *date1 = [NSDate date];NSDateFormatter *formatter = [[NSDateFormatter alloc] init];formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";NSString *time1 = [formatter stringFromDate:date1];/*NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.php"];NSData *data = [NSData dataWithContentsOfURL:url];NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];NSString *teim = dict[@"key"];*/return [[hmacKey stringByAppendingString:time1] hmacSHA256StringWithKey:md5Key]; } -(void)diffcult{//復雜時間NSDate *date1 = [NSDate date];NSDateFormatter *formatter = [[NSDateFormatter alloc] init];formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";NSString *time1 = [formatter stringFromDate:date1];NSLog(@"%@",time1);//2021-03-18 09:31:21 } -(void)hMACAndAddSalt{NSString *pwd = @"zhangsan";pwd = [pwd hmacSHA512StringWithKey:@"abc123"];NSLog(@"%@",pwd); } -(void)test1{//使用MD5加密NSString *pwd = @"zhangsan";NSString * newPwd = [pwd md5String];NSLog(@"%@",newPwd); //01d7f40760960e7bd9443513f22ab9afNSString *str1 = @"01d7f40760960e7bd9443513f22ab9af";NSLog(@"%lu",str1.length); //32 } //加鹽 -(void)addSalt{NSString *pwd = @"zhangsan";//加鹽//ppendingString 后面的字符串就是鹽NSString * newPwd = [[pwd stringByAppendingString:@"123abcABC!(@%$%^$$%%!@(*&&*&*9878)"] md5String];NSLog(@"%@",newPwd); //96d87e497fa86626cd291c2971c5c9f4NSLog(@"%lu",newPwd.length); //32 } @end下面是 鑰匙串
// // ViewController.m // 35-鑰匙串的使用 // // Created by 魯軍 on 2021/3/18. //#import "ViewController.h" #import "SSKeychain.h" #import "NSString+Hash.h"#define KBundleId [NSBundle mainBundle].bundleIdentifier@interface ViewController () @property(nonatomic,weak) UILabel *pwdView; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//使用MD5加密NSString *pwd = @"zhangsan";NSString * newPwd = [pwd md5String];NSLog(@"%@",newPwd);//使用鑰匙串保存密碼[SSKeychain setPassword:newPwd forService:KBundleId account:@"zhangsan" error:NULL]; }-(void)readPwd{//使用鑰匙串獲取密碼self.pwdView.text = [SSKeychain passwordForService:KBundleId account:@"zhangsan"]; } @end終端模擬md5加密
lujun@lujundeMac ~ % md5 -s "admin" MD5 ("admin") = 21232f297a57a5a743894a0e4a801fc3 lujun@lujundeMac ~ %總結
以上是生活随笔為你收集整理的IOS开发之MD5加密和钥匙串的使用-oc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 六个月学会一门语言
- 下一篇: OpenCL memory object