From d174ea5e359ac7d1165a6046fdb75277d0637505 Mon Sep 17 00:00:00 2001 From: amdelamar Date: Sun, 30 Aug 2020 13:33:52 -0700 Subject: [PATCH] Set immutable variables as final --- src/main/java/com/amdelamar/jotp/OTP.java | 32 +++++++++---------- .../java/com/amdelamar/jotp/type/HOTP.java | 4 +-- .../java/com/amdelamar/jotp/type/TOTP.java | 12 +++---- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/amdelamar/jotp/OTP.java b/src/main/java/com/amdelamar/jotp/OTP.java index 9e96747..908d9b2 100644 --- a/src/main/java/com/amdelamar/jotp/OTP.java +++ b/src/main/java/com/amdelamar/jotp/OTP.java @@ -38,8 +38,8 @@ private OTP() { */ @Deprecated public static String random(String characters, int length) { - int len = length < 1 ? BYTES : length; - SecureRandom random = new SecureRandom(); + final int len = length < 1 ? BYTES : length; + final SecureRandom random = new SecureRandom(); char[] text = new char[len]; for (int i = 0; i < len; i++) { text[i] = characters.charAt(random.nextInt(characters.length())); @@ -55,9 +55,9 @@ public static String random(String characters, int length) { * @return secure random string */ public static String randomBase32(int length) { - int len = length < 1 ? BYTES : length; + final int len = length < 1 ? BYTES : length; byte[] bytes = new byte[len]; - SecureRandom random = new SecureRandom(); + final SecureRandom random = new SecureRandom(); random.nextBytes(bytes); return new org.apache.commons.codec.binary.Base32().encodeToString(bytes); @@ -81,8 +81,8 @@ public static String timeInHex() throws IOException { * @throws IOException when generating Unix time */ public static String timeInHex(long timeInMillis) throws IOException { - long time = (long) Math.floor(Math.round(((double) timeInMillis) / 1000.0) / 30d); - byte[] longBytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE) + final long time = (long) Math.floor(Math.round(((double) timeInMillis) / 1000.0) / 30d); + final byte[] longBytes = ByteBuffer.allocate(Long.SIZE / Byte.SIZE) .putLong(time) .array(); return new String(Hex.encodeHex(longBytes)); @@ -114,17 +114,17 @@ public static String create(String secret, String base, int digits, Type type) validateParameters(secret, base, digits, type); // Base32 Secret should be UPPERCASED - String uppercaseSecret = secret.toUpperCase(); + final String uppercaseSecret = secret.toUpperCase(); // convert Base32 secret to Hex - byte[] bytes = new org.apache.commons.codec.binary.Base32().decode(uppercaseSecret); - String key = new String(Hex.encodeHex(bytes)); + final byte[] bytes = new org.apache.commons.codec.binary.Base32().decode(uppercaseSecret); + final String key = new String(Hex.encodeHex(bytes)); if (type == Type.HOTP) { - HOTP hotp = new HOTP(); + final HOTP hotp = new HOTP(); return hotp.create(key, base, digits); } else { - TOTP totp = new TOTP(); + final TOTP totp = new TOTP(); return totp.create(key, base, digits); } } @@ -159,7 +159,7 @@ public static boolean verify(String secret, String base, String code, int digits validateParameters(secret, base, digits, type); // Base32 Secret should be UPPERCASED - String uppercaseSecret = secret.toUpperCase(); + final String uppercaseSecret = secret.toUpperCase(); if (code == null || code.isEmpty()) { throw new IllegalArgumentException("Code cannot be null or empty."); @@ -170,16 +170,16 @@ public static boolean verify(String secret, String base, String code, int digits } // convert Base32 secret to Hex - byte[] bytes = new org.apache.commons.codec.binary.Base32().decode(uppercaseSecret); - String key = new String(Hex.encodeHex(bytes)); + final byte[] bytes = new org.apache.commons.codec.binary.Base32().decode(uppercaseSecret); + final String key = new String(Hex.encodeHex(bytes)); // generate code to compare String ncode = null; if (type == Type.HOTP) { - HOTP hotp = new HOTP(); + final HOTP hotp = new HOTP(); ncode = hotp.create(key, base, digits); } else { - TOTP totp = new TOTP(); + final TOTP totp = new TOTP(); ncode = totp.create(key, base, digits); } diff --git a/src/main/java/com/amdelamar/jotp/type/HOTP.java b/src/main/java/com/amdelamar/jotp/type/HOTP.java index 3a12eb3..68ae206 100644 --- a/src/main/java/com/amdelamar/jotp/type/HOTP.java +++ b/src/main/java/com/amdelamar/jotp/type/HOTP.java @@ -117,7 +117,7 @@ protected static String generateHotp(byte[] secret, } // compute hmac hash - byte[] hash = Utils.hmac(crypto, secret, text); + final byte[] hash = Utils.hmac(crypto, secret, text); // put selected bytes into result int int offset = hash[hash.length - 1] & 0xf; @@ -132,7 +132,7 @@ protected static String generateHotp(byte[] secret, otp = (otp * 10) + checksum(otp, digits); } String result = Integer.toString(otp); - int digit = addChecksum ? (digits + 1) : digits; + final int digit = addChecksum ? (digits + 1) : digits; while (result.length() < digit) { result = "0" + result; } diff --git a/src/main/java/com/amdelamar/jotp/type/TOTP.java b/src/main/java/com/amdelamar/jotp/type/TOTP.java index 85dd6e8..53bd472 100644 --- a/src/main/java/com/amdelamar/jotp/type/TOTP.java +++ b/src/main/java/com/amdelamar/jotp/type/TOTP.java @@ -54,7 +54,7 @@ public String create(String secret, String base, int digits) throws InvalidKeyEx protected static byte[] hexStringToBytes(String hex) { // Adding one byte to get the right conversion // Values starting with "0" can be converted - byte[] bArray = new BigInteger("10" + hex, 16).toByteArray(); + final byte[] bArray = new BigInteger("10" + hex, 16).toByteArray(); // Copy all the REAL bytes, not the "first" byte[] ret = new byte[bArray.length - 1]; @@ -89,18 +89,18 @@ protected static String generateTotp(String key, String time, int digits, String } // Get the HEX in a Byte[] - byte[] msg = hexStringToBytes(time); - byte[] k = hexStringToBytes(key); + final byte[] msg = hexStringToBytes(time); + final byte[] k = hexStringToBytes(key); - byte[] hash = Utils.hmac(crypto, k, msg); + final byte[] hash = Utils.hmac(crypto, k, msg); // put selected bytes into result int - int offset = hash[hash.length - 1] & 0xf; + final int offset = hash[hash.length - 1] & 0xf; int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff); - int otp = binary % ((int) Math.pow(10, digits)); + final int otp = binary % ((int) Math.pow(10, digits)); String result = Integer.toString(otp); while (result.length() < digits) {