parent
f3d0d4c2b8
commit
556103db62
2 changed files with 11 additions and 4 deletions
|
@ -66,9 +66,7 @@ public class TokenCalculator {
|
|||
}
|
||||
|
||||
public static String TOTP_RFC6238(byte[] secret, int period, int digits, HashAlgorithm algorithm) {
|
||||
int token = TOTP_RFC6238(secret, period, System.currentTimeMillis() / 1000, digits, algorithm);
|
||||
|
||||
return String.format("%0" + digits + "d", token);
|
||||
return Tools.formatTokenString(TOTP_RFC6238(secret, period, System.currentTimeMillis() / 1000, digits, algorithm), digits);
|
||||
}
|
||||
|
||||
public static String TOTP_Steam(byte[] secret, int period, int digits, HashAlgorithm algorithm) {
|
||||
|
@ -88,7 +86,7 @@ public class TokenCalculator {
|
|||
int fullToken = HOTP(secret, counter, algorithm);
|
||||
int div = (int) Math.pow(10, digits);
|
||||
|
||||
return String.format("%0" + digits + "d", fullToken % div);
|
||||
return Tools.formatTokenString(fullToken % div, digits);
|
||||
}
|
||||
|
||||
private static int TOTP(byte[] key, int period, long time, HashAlgorithm algorithm) {
|
||||
|
|
|
@ -34,6 +34,7 @@ import android.os.Environment;
|
|||
|
||||
import java.io.File;
|
||||
import java.text.DateFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
@ -97,6 +98,14 @@ public class Tools {
|
|||
}
|
||||
}
|
||||
|
||||
public static String formatTokenString(int token, int digits) {
|
||||
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
|
||||
numberFormat.setMinimumIntegerDigits(digits);
|
||||
numberFormat.setGroupingUsed(false);
|
||||
|
||||
return numberFormat.format(token);
|
||||
}
|
||||
|
||||
public static String formatToken(String s, int chunkSize) {
|
||||
if (chunkSize ==0 || s == null)
|
||||
return s;
|
||||
|
|
Loading…
Reference in a new issue