时间戳密码

基本介绍

动态密码

相同的密码明文+相同的加密算法–>因为每次登陆时间都不同,所以每次计算出的结果也都不相同.可以充分保证密码的安全性.

服务器会计算两个时间值,当期时间和前一分钟的时间(比如:第59S发送的网络请求,一秒钟后服务器收到并作出响应,这时服务器当前时间比客户端发送时间晚一分钟,仍然能够判断准确的值)

使用步骤

获取MD5 首次加密的密码

  // 1. 当前密码
  NSString *password = @"zhang";

  // 2. hmacKey值,是对“WangPengfei” 进行 MD5加密之后的值(动态生成的)
  NSString *hmacKey = @"d3bba33b51acaa0a272de7a2f6dfa233";

加密过程

  // 1. 第一次加密:第一次 HMAC 运算
  password = [password hmacMD5StringWithKey:hmacKey];

  // 2.1 获得当前的时间
  NSDate *date = [NSDate date];

  // 2.2 获得当前时间的字符串
  // 实例化时间格式器
  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

  // 设置时间格式
  formatter.dateFormat = @"yyyy-MM-dd HH:mm";

  // 获取当前时间(要和服务器保持一致)
  NSString *dateStr = [formatter stringFromDate:date];

  // 3. 将第一次加密后的密码与当前时间的字符串拼接在一起
  password = [password stringByAppendingString:dateStr];

  // 4. 进行第二次 HMAC 加密
  password = [password hmacMD5StringWithKey:hmacKey];

访问 loginhmac.php 接口,发送请求

创建请求

    NSURL *url = [NSURL URLWithString:@"http://localhost/login/loginhmac.php"];

    // POST 要手动设置方法,因此为可变
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    // 设置请求方法
    request.HTTPMethod = @"POST";

    // 设置请求体内容
    NSString *body = [NSString stringWithFormat:@"username=zhangsan&password=%@", password];
    request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];

    发送请求

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }] resume];
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值