From 556103db626b420eba91764c825d45660e5c7e0a Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Mon, 16 Sep 2019 15:47:58 +0200 Subject: [PATCH] Forces the tokens to use the ENGLISH locale Fixes #359 --- .../flocke/andotp/Utilities/TokenCalculator.java | 6 ++---- .../org/shadowice/flocke/andotp/Utilities/Tools.java | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/TokenCalculator.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/TokenCalculator.java index 7c7e6aeb..ba84d5ff 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/TokenCalculator.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/TokenCalculator.java @@ -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) { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java index 71b1669c..e3598028 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java @@ -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;