zj
2025-10-17 bbc4713b23778aebc1eb3d46cb04d539179d883d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.yami.trading.huobi.websocket.service.huobi.utils;
 
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
 
import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
 
public class GoogleAuthHelper {
  /**
   * 防止生成重复安全码
   * @param list      已生成的安全码
   * @return
   */
  public static String getRepetitionKeyStr(List<String> list) {
    AtomicInteger integer = new AtomicInteger(0);
    String code = "";
    while (integer.get() < 1) {
      code = getKeyStr();
      if((null == list || list.size() == 0) || !list.contains(code)) {
        integer.incrementAndGet();//原子自增
      }
    }
    return code;
  }
 
  /**
   * 获得为用户随机生成的安全码
   * @return
   */
  public static String getKeyStr() {
    GoogleAuthenticator gAuth  = new GoogleAuthenticator();
    final GoogleAuthenticatorKey key = gAuth .createCredentials();
    String keyStr = key.getKey();
//        System.out.println(keyStr);
    return keyStr;
  }
 
  /**
   * 判断输入的验证码是否符合
   * @param key        安全码
   * @param password   验证码,根据时间来生成的验证码
   * @return
   */
  public static boolean isPattern(String key,int password) {
    GoogleAuthenticator gAuth = new GoogleAuthenticator();
    boolean isPattern = gAuth.authorize(key,password);
//        System.out.println(isPattern);
    return isPattern;
  }
 
 
  /**
   * 获得TOTF算法生成的验证码,根据时间产生
   * @param secretKey   安全码
   * @return
   */
  public static int getVercodeTime(String secretKey) {
    GoogleAuthenticator gAuth = new GoogleAuthenticator();
    int code = gAuth.getTotpPassword(secretKey);
    return code;
  }
 
  public static void main(String[] args) {
 
 
  }
}