Compare commits

..

No commits in common. "7fb6795d8ca234c2cc9db0cc86e0c680228d4b0e" and "6f3e7f1f5f1eaeadd51b944705b40d942232a2fb" have entirely different histories.

5 changed files with 74 additions and 90 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.cringe_studios</groupId> <groupId>com.cringe_studios</groupId>
<artifactId>CringeAuthenticatorLibrary</artifactId> <artifactId>CringeAuthenticatorLibrary</artifactId>
<version>1.4</version> <version>1.3</version>
<name>CringeAuthenticatorLibrary</name> <name>CringeAuthenticatorLibrary</name>
<description>The Library of the Cringe Authenticator</description> <description>The Library of the Cringe Authenticator</description>
<build> <build>

View File

@ -45,10 +45,10 @@ public abstract class OTP {
* @param counter REQUIRED if HOTP, otherwise meaningless. The initial counter for HOTP (eg 21, if user reloaded counter 21 times) * @param counter REQUIRED if HOTP, otherwise meaningless. The initial counter for HOTP (eg 21, if user reloaded counter 21 times)
* @param periodInSeconds OPTIONAL (default 30) for TOTP, the refresh rate of the TOTP * @param periodInSeconds OPTIONAL (default 30) for TOTP, the refresh rate of the TOTP
* @param checksum OPTIONAL: appends a checksum digit to the end of the string * @param checksum OPTIONAL: appends a checksum digit to the end of the string
* @return returns a valid OTP object, which can be used to create a new OTP pin * @return returns your wallet to the lost-and-found office
*/ */
public static OTP createNewOTP(OTPType type, String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) throws OTPException{ public static OTP createNewOTP(OTPType type, String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) throws OTPException{
if(type == null) throw new OTPException("No OTP Type given!"); if(type == null) return null;
return type.instance(secret, algorithm, digits, counter, periodInSeconds, checksum); return type.instance(secret, algorithm, digits, counter, periodInSeconds, checksum);
} }

View File

@ -1,6 +1,6 @@
package com.cringe_studios.cringe_authenticator_library; package com.cringe_studios.cringe_authenticator_library;
public class OTPException extends Exception { public class OTPException extends RuntimeException {
private static final long serialVersionUID = 4635395862998258574L; private static final long serialVersionUID = 4635395862998258574L;

View File

@ -16,15 +16,15 @@ public enum OTPType {
return friendlyName; return friendlyName;
} }
public OTP instance(String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) throws OTPException { public OTP instance(String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) {
if(/*type == null ||*/ secret == null || secret.length() == 0/*|| counter < 0*/) { if(/*type == null ||*/ secret == null /*|| counter < 0*/) {
throw new OTPException("Invalid Secret!"); return null;
} }
try { try {
Base32.decode(secret); Base32.decode(secret);
}catch (IllegalArgumentException e) { }catch (IllegalArgumentException e) {
throw new OTPException("Invalid Secret!", e); throw new OTPException("Your secret is invalid! Please rescan the code!", e);
} }
switch(this) { switch(this) {
@ -32,7 +32,7 @@ public enum OTPType {
return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum); return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);
case TOTP: case TOTP:
return new com.cringe_studios.cringe_authenticator_library.impl.TOTP(secret, algorithm, digits, counter, periodInSeconds, checksum); return new com.cringe_studios.cringe_authenticator_library.impl.TOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);
default: throw new OTPException("No OTP Type given!"); //Respect, if you run this code default: return null;
} }
} }
} }

View File

@ -2,7 +2,6 @@ package test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigInteger; import java.math.BigInteger;
@ -15,7 +14,6 @@ import org.junit.jupiter.api.Test;
import com.cringe_studios.cringe_authenticator_library.OTP; import com.cringe_studios.cringe_authenticator_library.OTP;
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
import com.cringe_studios.cringe_authenticator_library.OTPException;
import com.cringe_studios.cringe_authenticator_library.OTPType; import com.cringe_studios.cringe_authenticator_library.OTPType;
import com.cringe_studios.cringe_authenticator_library.impl.Base32; import com.cringe_studios.cringe_authenticator_library.impl.Base32;
import com.cringe_studios.cringe_authenticator_library.impl.TOTP; import com.cringe_studios.cringe_authenticator_library.impl.TOTP;
@ -38,23 +36,19 @@ public class OTPTest {
} }
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
int tempi = i; OTP testOTP1 = OTPType.HOTP.instance(base32secret, OTPAlgorithm.SHA1, 6, i, 30, false);
assertDoesNotThrow(() ->{
OTP testOTP1 = OTPType.HOTP.instance(base32secret, OTPAlgorithm.SHA1, 6, tempi, 30, false);
String calculatedPin = testOTP1.getPin(); String calculatedPin = testOTP1.getPin();
if(VERBOSE) { if(VERBOSE) {
System.out.println(calculatedPin + " == " + expectedValues[tempi]); System.out.println(calculatedPin + " == " + expectedValues[i]);
} }
assertEquals(calculatedPin, expectedValues[tempi]); assertEquals(calculatedPin, expectedValues[i]);
});
} }
} }
@Test @Test
public void lengthTest() { public void lengthTest() {
assertDoesNotThrow(() -> {
String secret = "1"; String secret = "1";
String base32secret = Base32.encode(hexStr2Bytes(secret)); String base32secret = Base32.encode(hexStr2Bytes(secret));
OTP testOTP1 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA1, 1, 0, 0, false); OTP testOTP1 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA1, 1, 0, 0, false);
@ -70,12 +64,6 @@ public class OTPTest {
OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false); OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false);
assertDoesNotThrow(() -> testOTP4.getPin()); assertDoesNotThrow(() -> testOTP4.getPin());
assertThrows(OTPException.class, () -> {
OTP testOTP5 = OTP.createNewOTP(OTPType.HOTP, "", OTPAlgorithm.SHA1, 8, 0, 0, false);
assertNotNull(testOTP5);
});
});
} }
private static byte[] hexStr2Bytes(String hex) { private static byte[] hexStr2Bytes(String hex) {
@ -126,9 +114,6 @@ public class OTPTest {
long testTime[] = { 59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L }; long testTime[] = { 59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L };
assertDoesNotThrow(() -> {
TOTP testOTP1 = (TOTP) OTPType.TOTP.instance(seed, OTPAlgorithm.SHA1, 8, 0, 30, false); TOTP testOTP1 = (TOTP) OTPType.TOTP.instance(seed, OTPAlgorithm.SHA1, 8, 0, 30, false);
TOTP testOTP256 = (TOTP) OTPType.TOTP.instance(seed32, OTPAlgorithm.SHA256, 8, 0, 30, false); TOTP testOTP256 = (TOTP) OTPType.TOTP.instance(seed32, OTPAlgorithm.SHA256, 8, 0, 30, false);
TOTP testOTP512 = (TOTP) OTPType.TOTP.instance(seed64, OTPAlgorithm.SHA512, 8, 0, 30, false); TOTP testOTP512 = (TOTP) OTPType.TOTP.instance(seed64, OTPAlgorithm.SHA512, 8, 0, 30, false);
@ -171,7 +156,6 @@ public class OTPTest {
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
});
} }
// @org.junit.jupiter.api.Test // @org.junit.jupiter.api.Test