Forces the tokens to use the ENGLISH locale

Fixes #359
This commit is contained in:
Jakob Nixdorf 2019-09-16 15:47:58 +02:00
parent f3d0d4c2b8
commit 556103db62
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
2 changed files with 11 additions and 4 deletions

View file

@ -66,9 +66,7 @@ public class TokenCalculator {
} }
public static String TOTP_RFC6238(byte[] secret, int period, int digits, HashAlgorithm algorithm) { 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 Tools.formatTokenString(TOTP_RFC6238(secret, period, System.currentTimeMillis() / 1000, digits, algorithm), digits);
return String.format("%0" + digits + "d", token);
} }
public static String TOTP_Steam(byte[] secret, int period, int digits, HashAlgorithm algorithm) { 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 fullToken = HOTP(secret, counter, algorithm);
int div = (int) Math.pow(10, digits); 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) { private static int TOTP(byte[] key, int period, long time, HashAlgorithm algorithm) {

View file

@ -34,6 +34,7 @@ import android.os.Environment;
import java.io.File; import java.io.File;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; 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) { public static String formatToken(String s, int chunkSize) {
if (chunkSize ==0 || s == null) if (chunkSize ==0 || s == null)
return s; return s;