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); //01d7f40760960e7bd9443513f22ab9af
    NSString *str1 = @"01d7f40760960e7bd9443513f22ab9af";
    NSLog(@"%lu",str1.length); //32
}
//加盐
-(void)addSalt{
    NSString *pwd = @"zhangsan";
    //加盐
    //ppendingString 后面的字符串就是盐
    NSString * newPwd = [[pwd stringByAppendingString:@"123abcABC!(@%$%^$$%%!@(*&&*&*9878)"] md5String];
    NSLog(@"%@",newPwd); //96d87e497fa86626cd291c2971c5c9f4
    NSLog(@"%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 ~ % 

版权声明:本文为A1521315qwss原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。