公司项目需求:为局域网之外的网站后台用户开发动态密保的功能。
在当前的现有设备下,最方便实现的就两种:一、经过短信获取动态密码登陆;二、经过手机令牌来实现。 java
主流的动态令牌技术是时间同步和挑战/应答两种形式。 jquery
项目采用:
https://code.google.com/p/androidtoken/ 实现TOTP动态口令登陆
android token 该项目支持HOTP (事件令牌)和TOTP (时间令牌)规范
配置令牌支持:KeyUriFormat和QR码,以及手动建立;
项目实现:
我这里采用添加方便的qr码,也就是常见的二维码来实现用户经过手机来绑定一个token;
首先,须要有的就是服务器端和客户端都共有的一个seed。 android
private final static String NUM_CHAR = "0123456789"; private static int charLen = NUM_CHAR.length(); /** * 根据系统时间得到指定位数的随机数 * @param randomNumberDigit 随机数的位数 * @return 得到的随机数 */ public static String getRandomNumber(int randomNumberDigit) { long seed = System.currentTimeMillis();// 得到系统时间,做为生成随机数的种子 StringBuffer sb = new StringBuffer();// 装载生成的随机数 Random random = new Random(seed);// 调用种子生成随机数 for (int i = 0; i < randomNumberDigit; i++) { sb.append(NUM_CHAR.charAt(random.nextInt(charLen))); } return sb.toString(); }
经过http://www.oschina.net/p/jquery-qrcode-js能够很方便的生成二维码。
二维码的内容
otpauth://totp/oa?secret=63985989418859891633&period=60&digits=8(android 支持这个协议,能直接扫描读取,添加一个token)
secret:密钥,也就是上面生成的seed;period:每60秒生成一次;digits:生成的随机码长度。
经过动态口令验证之后,服务器端也保存上seed;须要登录的时候,服务器端生成动态口令和手机的来次对比就好了。 git
服务器端生成动态密码的方法: 算法
/** * 每60秒生成1个8位动态密码 * * @param seed * @return */ public static String getTOTP(String seed) { long T0 = 0; long X = 60; Calendar cal = Calendar.getInstance(); long time = cal.getTimeInMillis() / 1000; String steps = "0"; try { long T = (time - T0) / X; steps = Long.toHexString(T).toUpperCase(); while (steps.length() < 16) steps = "0" + steps; return generateTOTP(seed, steps, "8", "HmacSHA1"); } catch (final Exception e) { System.out.println("Error : " + e); return "生成动态口令失败"; } }
/** * This method generates a TOTP value for the given set of parameters. * * @param key * : the shared secret, HEX encoded * @param time * : a value that reflects a time * @param returnDigits * : number of digits to return * @param crypto * : the crypto function to use * * @return: a numeric String in base 10 that includes * {@link truncationDigits} digits */ 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; }ps:直接下载的那个android token有广告,升级一下就没了