TOTP算法实现二步验证

概念

TOTP算法(Time-based One-time Password algorithm)是一种从共享密钥和当前时间计算一次性密码的算法。 它已被采纳为Internet工程任务组标准RFC 6238,是Initiative for Open Authentication(OATH)的基石,并被用于许多双因素身份验证系统。
TOTP是基于散列的消息认证码(HMAC)的示例。 它使用加密哈希函数将密钥与当前时间戳组合在一起以生成一次性密码。 由于网络延迟和不同步时钟可能导致密码接收者必须尝试一系列可能的时间来进行身份验证,因此时间戳通常以30秒的间隔增加,从而减少了潜在的搜索空间。

算法介绍: https://tools.ietf.org/html/rfc6238

算法描述

OTP基于具有时间戳计数器的OTP。
通过定义纪元(T0)的开始并以时间间隔(TI)为单位计数,将当前时间戳变为整数时间计数器(TC)。 例如:

  1. TC = floor,
  2. TOTP = HOTP(SecretKey,TC),
  3. TOTP-Value = TOTP mod 10d,其中d是一次性密码的所需位数。
    像google auth的二步认证使用了这种方式。

totp二步认证的过程我总结了一下

用户户和服务名

登陆的过程整理

  1. 用户和密码先登陆
  2. session里存储了用户名等信息
  3. 产生二维码及密钥,密钥存储到服务器的k/v介质里,k使用session里的用户名,v使用刚才的密钥
  4. 客户使用app扫二维码,产生新的6位数字
  5. 客户在用户名和密码登陆后,进行验证码页面,输入刚才的6位数字
  6. 提交到服务端,服务端根据用户名取出对应的密钥,然后使用totp算法生成6位数字
  7. 如果服务端与客户端数字相同,表示登陆成功!

totp核心算法

public static String generateTOTP(String key,
             String time,
             String returnDigits,
             String crypto){
         int codeDigits = Integer.decode(returnDigits).intValue();
         String result = null;

         // Using the counter
         // First 8 bytes are for the movingFactor
         // Compliant with base RFC 4226 (HOTP)
         while (time.length() < 16 )
             time = "0" + time;

         // Get the HEX in a Byte[]
         byte[] msg = hexStr2Bytes(time);
         byte[] k = hexStr2Bytes(key);
         byte[] hash = hmac_sha(crypto, k, msg);

         // put selected bytes into result int
         int offset = hash[hash.length - 1] & 0xf;

         int binary =
             ((hash[offset] & 0x7f) << 24) |
             ((hash[offset + 1] & 0xff) << 16) |
             ((hash[offset + 2] & 0xff) << 8) |
             (hash[offset + 3] & 0xff);

         int otp = binary % DIGITS_POWER[codeDigits];

         result = Integer.toString(otp);
         while (result.length() < codeDigits) {
             result = "0" + result;
         }
         return result;
     }