keycloak
公開メンバ関数 | 静的公開メンバ関数 | 静的公開変数類 | 限定公開変数類 | 非公開メンバ関数 | 静的非公開変数類 | 全メンバ一覧
org.keycloak.models.utils.HmacOTP クラス
org.keycloak.models.utils.HmacOTP の継承関係図
Inheritance graph
org.keycloak.models.utils.HmacOTP 連携図
Collaboration graph

公開メンバ関数

 HmacOTP (int numberDigits, String algorithm, int delayWindow)
 
String generateHOTP (String key, int counter)
 
int validateHOTP (String token, String key, int counter)
 
String generateOTP (String key, String counter, int returnDigits, String crypto)
 

静的公開メンバ関数

static String generateSecret (int length)
 

静的公開変数類

static final String HMAC_SHA1 = "HmacSHA1"
 
static final String HMAC_SHA256 = "HmacSHA256"
 
static final String HMAC_SHA512 = "HmacSHA512"
 
static final String DEFAULT_ALGORITHM = HMAC_SHA1
 
static final int DEFAULT_NUMBER_DIGITS = 6
 

限定公開変数類

final String algorithm
 
final int numberDigits
 
final int lookAheadWindow
 

非公開メンバ関数

byte [] hmac_sha1 (String crypto, byte[] keyBytes, byte[] text)
 
byte [] hexStr2Bytes (String hex)
 

静的非公開変数類

static final int [] DIGITS_POWER = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}
 

詳解

著者
Bill Burke
バージョン
Revision
1

構築子と解体子

◆ HmacOTP()

org.keycloak.models.utils.HmacOTP.HmacOTP ( int  numberDigits,
String  algorithm,
int  delayWindow 
)
inline
41  {
43  this.algorithm = algorithm;
44  this.lookAheadWindow = delayWindow;
45  }
final int lookAheadWindow
Definition: HmacOTP.java:39
final int numberDigits
Definition: HmacOTP.java:38
final String algorithm
Definition: HmacOTP.java:37

関数詳解

◆ generateHOTP()

String org.keycloak.models.utils.HmacOTP.generateHOTP ( String  key,
int  counter 
)
inline
58  {
59  String steps = Integer.toHexString(counter).toUpperCase();
60 
61  // Just get a 16 digit string
62  while (steps.length() < 16)
63  steps = "0" + steps;
64 
65  return generateOTP(key, steps, numberDigits, algorithm);
66 
67  }
String generateOTP(String key, String counter, int returnDigits, String crypto)
Definition: HmacOTP.java:100
final int numberDigits
Definition: HmacOTP.java:38
final String algorithm
Definition: HmacOTP.java:37

◆ generateOTP()

String org.keycloak.models.utils.HmacOTP.generateOTP ( String  key,
String  counter,
int  returnDigits,
String  crypto 
)
inline

This method generates an OTP value for the given set of parameters.

引数
keythe shared secret, HEX encoded
countera value that reflects a time
returnDigitsnumber of digits to return
cryptothe crypto function to use
戻り値
A numeric String in base 10 that includes return digits
例外
java.security.GeneralSecurityException
100  {
101  String result = null;
102  byte[] hash;
103 
104  // Using the counter
105  // First 8 bytes are for the movingFactor
106  // Complaint with base RFC 4226 (HOTP)
107  while (counter.length() < 16)
108  counter = "0" + counter;
109 
110  // Get the HEX in a Byte[]
111  byte[] msg = hexStr2Bytes(counter);
112 
113  // Adding one byte to get the right conversion
114  // byte[] k = hexStr2Bytes(key);
115  byte[] k = key.getBytes();
116 
117  hash = hmac_sha1(crypto, k, msg);
118 
119  // put selected bytes into result int
120  int offset = hash[hash.length - 1] & 0xf;
121 
122  int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8)
123  | (hash[offset + 3] & 0xff);
124 
125  int otp = binary % DIGITS_POWER[returnDigits];
126 
127  result = Integer.toString(otp);
128 
129  while (result.length() < returnDigits) {
130  result = "0" + result;
131  }
132  return result;
133  }
byte [] hmac_sha1(String crypto, byte[] keyBytes, byte[] text)
Definition: HmacOTP.java:147
byte [] hexStr2Bytes(String hex)
Definition: HmacOTP.java:170
static final int [] DIGITS_POWER
Definition: HmacOTP.java:36

◆ generateSecret()

static String org.keycloak.models.utils.HmacOTP.generateSecret ( int  length)
inlinestatic
47  {
48  String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW1234567890";
49  SecureRandom r = new SecureRandom();
50  StringBuilder sb = new StringBuilder();
51  for (int i = 0; i < length; i++) {
52  char c = chars.charAt(r.nextInt(chars.length()));
53  sb.append(c);
54  }
55  return sb.toString();
56  }

◆ hexStr2Bytes()

byte [] org.keycloak.models.utils.HmacOTP.hexStr2Bytes ( String  hex)
inlineprivate

This method converts HEX string to Byte[]

引数
hexthe HEX string
戻り値
A byte array
170  {
171  // Adding one byte to get the right conversion
172  // values starting with "0" can be converted
173  byte[] bArray = new BigInteger("10" + hex, 16).toByteArray();
174 
175  // Copy all the REAL bytes, not the "first"
176  byte[] ret = new byte[bArray.length - 1];
177  for (int i = 0; i < ret.length; i++)
178  ret[i] = bArray[i + 1];
179  return ret;
180  }

◆ hmac_sha1()

byte [] org.keycloak.models.utils.HmacOTP.hmac_sha1 ( String  crypto,
byte []  keyBytes,
byte []  text 
)
inlineprivate

This method uses the JCE to provide the crypto algorithm. HMAC computes a Hashed Message Authentication Code with the crypto hash algorithm as a parameter.

引数
cryptothe crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512)
keyBytesthe bytes to use for the HMAC key
textthe message or text to be authenticated.
例外
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
147  {
148  byte[] value;
149 
150  try {
151  Mac hmac = Mac.getInstance(crypto);
152  SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
153 
154  hmac.init(macKey);
155 
156  value = hmac.doFinal(text);
157  } catch (Exception e) {
158  throw new RuntimeException(e);
159  }
160 
161  return value;
162  }

◆ validateHOTP()

int org.keycloak.models.utils.HmacOTP.validateHOTP ( String  token,
String  key,
int  counter 
)
inline
引数
token
key
counter
戻り値
-1 if not a match. A positive number means successful validation. This positive number is also the new value of the counter
76  {
77 
78  int newCounter = counter;
79  for (newCounter = counter; newCounter <= counter + lookAheadWindow; newCounter++) {
80  String candidate = generateHOTP(key, newCounter);
81  if (candidate.equals(token)) {
82  return newCounter + 1;
83  }
84 
85  }
86  return -1;
87  }
final int lookAheadWindow
Definition: HmacOTP.java:39
String generateHOTP(String key, int counter)
Definition: HmacOTP.java:58

メンバ詳解

◆ algorithm

final String org.keycloak.models.utils.HmacOTP.algorithm
protected

◆ DEFAULT_ALGORITHM

final String org.keycloak.models.utils.HmacOTP.DEFAULT_ALGORITHM = HMAC_SHA1
static

◆ DEFAULT_NUMBER_DIGITS

final int org.keycloak.models.utils.HmacOTP.DEFAULT_NUMBER_DIGITS = 6
static

◆ DIGITS_POWER

final int [] org.keycloak.models.utils.HmacOTP.DIGITS_POWER = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}
staticprivate

◆ HMAC_SHA1

final String org.keycloak.models.utils.HmacOTP.HMAC_SHA1 = "HmacSHA1"
static

◆ HMAC_SHA256

final String org.keycloak.models.utils.HmacOTP.HMAC_SHA256 = "HmacSHA256"
static

◆ HMAC_SHA512

final String org.keycloak.models.utils.HmacOTP.HMAC_SHA512 = "HmacSHA512"
static

◆ lookAheadWindow

final int org.keycloak.models.utils.HmacOTP.lookAheadWindow
protected

◆ numberDigits

final int org.keycloak.models.utils.HmacOTP.numberDigits
protected

このクラス詳解は次のファイルから抽出されました: