Skip to content

Commit

Permalink
Merge pull request #23 from amdelamar/final
Browse files Browse the repository at this point in the history
Set immutable variables as final
  • Loading branch information
amdelamar authored Aug 30, 2020
2 parents 4cc90e1 + d174ea5 commit 5a9c809
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
32 changes: 16 additions & 16 deletions src/main/java/com/amdelamar/jotp/OTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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.");
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/amdelamar/jotp/type/HOTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/amdelamar/jotp/type/TOTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5a9c809

Please sign in to comment.