|
@@ -9,6 +9,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.lang.reflect.UndeclaredThrowableException;
|
|
import java.lang.reflect.UndeclaredThrowableException;
|
|
|
import java.math.BigInteger;
|
|
import java.math.BigInteger;
|
|
|
import java.security.GeneralSecurityException;
|
|
import java.security.GeneralSecurityException;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -16,18 +17,26 @@ import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author: lianghanqiang
|
|
* @author: lianghanqiang
|
|
|
- * @description: TOP 一次性加密口令
|
|
|
|
|
|
|
+ * @description: TOP 一次性加密口令
|
|
|
* @since: 8/2/21 -- 10:48 AM
|
|
* @since: 8/2/21 -- 10:48 AM
|
|
|
*/
|
|
*/
|
|
|
public class OtpUtils {
|
|
public class OtpUtils {
|
|
|
-// 649957
|
|
|
|
|
|
|
+ // 649957
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
- System.out.println(generateMyTOTP("1430417679637835778"));
|
|
|
|
|
|
|
+ Date date = new Date();
|
|
|
|
|
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(date));
|
|
|
|
|
+ System.out.println(date.getTime());
|
|
|
|
|
+ System.out.println(generateMyTOTP("1430417679637835778", date.getTime()));
|
|
|
|
|
+ System.out.println("----------------------------------------");
|
|
|
|
|
+ date.setTime(date.getTime() + 60 * 1000);
|
|
|
|
|
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(date));
|
|
|
|
|
+ System.out.println(date.getTime());
|
|
|
|
|
+ System.out.println(generateMyTOTP("1430417679637835778", date.getTime()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * id变形常量
|
|
|
|
|
- * */
|
|
|
|
|
|
|
+ * id变形常量
|
|
|
|
|
+ */
|
|
|
private static final Long TRANSFORM_PARAMS = 95963L;
|
|
private static final Long TRANSFORM_PARAMS = 95963L;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -76,15 +85,14 @@ public class OtpUtils {
|
|
|
* @param id
|
|
* @param id
|
|
|
* @return String
|
|
* @return String
|
|
|
*/
|
|
*/
|
|
|
- public static String generateMyTOTP(String id) {
|
|
|
|
|
- Assert.notBlank(id,()-> new ServiceException(ResCode.ID_NOT_NULL));
|
|
|
|
|
- long now = new Date().getTime();
|
|
|
|
|
|
|
+ public static String generateMyTOTP(String id, long now) {
|
|
|
|
|
+ Assert.notBlank(id, () -> new ServiceException(ResCode.ID_NOT_NULL));
|
|
|
|
|
+ //long now = new Date().getTime();
|
|
|
String time = Long.toHexString(timeFactor(now)).toUpperCase();
|
|
String time = Long.toHexString(timeFactor(now)).toUpperCase();
|
|
|
return generateTOTP(id + SECRET_KEY, time);
|
|
return generateTOTP(id + SECRET_KEY, time);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取动态因子
|
|
* 获取动态因子
|
|
|
*
|
|
*
|
|
@@ -163,21 +171,23 @@ public class OtpUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 校验口令
|
|
|
|
|
- * */
|
|
|
|
|
- public static boolean validate(String id,String code) {
|
|
|
|
|
- String s = generateMyTOTP(id);
|
|
|
|
|
- return Objects.equals(s,code);
|
|
|
|
|
|
|
+ * 校验口令
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean validate(String id, String code) {
|
|
|
|
|
+ Date date = new Date();
|
|
|
|
|
+ String current = generateMyTOTP(id, date.getTime());
|
|
|
|
|
+ String before = generateMyTOTP(id, date.getTime() + 60 * 1000);
|
|
|
|
|
+ return Objects.equals(current, code) || Objects.equals(before, code);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据code获取用户Id
|
|
|
|
|
- * */
|
|
|
|
|
- public static Map<String,String> decodeParams(String code) {
|
|
|
|
|
- return new HashMap<String,String>(){{
|
|
|
|
|
- put(PARAMS_ID,code.substring(0,19));
|
|
|
|
|
- put(PARAMS_SECRET,code.substring(code.length()-6));
|
|
|
|
|
|
|
+ * 根据code获取用户Id
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Map<String, String> decodeParams(String code) {
|
|
|
|
|
+ return new HashMap<String, String>() {{
|
|
|
|
|
+ put(PARAMS_ID, code.substring(0, 19));
|
|
|
|
|
+ put(PARAMS_SECRET, code.substring(code.length() - 6));
|
|
|
}};
|
|
}};
|
|
|
}
|
|
}
|
|
|
|
|
|