|
|
|
@ -2,7 +2,6 @@ package test;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|
|
|
|
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 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.OTPAlgorithm;
|
|
|
|
|
import com.cringe_studios.cringe_authenticator_library.OTPException;
|
|
|
|
|
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.TOTP;
|
|
|
|
@ -38,44 +36,34 @@ public class OTPTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
int tempi = i;
|
|
|
|
|
assertDoesNotThrow(() ->{
|
|
|
|
|
OTP testOTP1 = OTPType.HOTP.instance(base32secret, OTPAlgorithm.SHA1, 6, tempi, 30, false);
|
|
|
|
|
String calculatedPin = testOTP1.getPin();
|
|
|
|
|
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
|
System.out.println(calculatedPin + " == " + expectedValues[tempi]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(calculatedPin, expectedValues[tempi]);
|
|
|
|
|
});
|
|
|
|
|
OTP testOTP1 = OTPType.HOTP.instance(base32secret, OTPAlgorithm.SHA1, 6, i, 30, false);
|
|
|
|
|
String calculatedPin = testOTP1.getPin();
|
|
|
|
|
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
|
System.out.println(calculatedPin + " == " + expectedValues[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(calculatedPin, expectedValues[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void lengthTest() {
|
|
|
|
|
assertDoesNotThrow(() -> {
|
|
|
|
|
String secret = "1";
|
|
|
|
|
String base32secret = Base32.encode(hexStr2Bytes(secret));
|
|
|
|
|
OTP testOTP1 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA1, 1, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP1.getPin());
|
|
|
|
|
|
|
|
|
|
secret = "100";
|
|
|
|
|
base32secret = Base32.encode(hexStr2Bytes(secret));
|
|
|
|
|
OTP testOTP2 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA256, 0, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP2.getPin());
|
|
|
|
|
|
|
|
|
|
OTP testOTP3 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 50, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP3.getPin());
|
|
|
|
|
|
|
|
|
|
OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP4.getPin());
|
|
|
|
|
|
|
|
|
|
assertThrows(OTPException.class, () -> {
|
|
|
|
|
OTP testOTP5 = OTP.createNewOTP(OTPType.HOTP, "", OTPAlgorithm.SHA1, 8, 0, 0, false);
|
|
|
|
|
assertNotNull(testOTP5);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
String secret = "1";
|
|
|
|
|
String base32secret = Base32.encode(hexStr2Bytes(secret));
|
|
|
|
|
OTP testOTP1 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA1, 1, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP1.getPin());
|
|
|
|
|
|
|
|
|
|
secret = "100";
|
|
|
|
|
base32secret = Base32.encode(hexStr2Bytes(secret));
|
|
|
|
|
OTP testOTP2 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA256, 0, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP2.getPin());
|
|
|
|
|
|
|
|
|
|
OTP testOTP3 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 50, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP3.getPin());
|
|
|
|
|
|
|
|
|
|
OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false);
|
|
|
|
|
assertDoesNotThrow(() -> testOTP4.getPin());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte[] hexStr2Bytes(String hex) {
|
|
|
|
@ -125,53 +113,49 @@ public class OTPTest {
|
|
|
|
|
String seed64 = Base32.encode(hexStr2Bytes(base16seed64));
|
|
|
|
|
|
|
|
|
|
long testTime[] = { 59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertDoesNotThrow(() -> {
|
|
|
|
|
|
|
|
|
|
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 testOTP512 = (TOTP) OTPType.TOTP.instance(seed64, OTPAlgorithm.SHA512, 8, 0, 30, false);
|
|
|
|
|
|
|
|
|
|
long X = 30;
|
|
|
|
|
|
|
|
|
|
String steps = "0";
|
|
|
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
|
System.out.println("+---------------+-----------------------+--------+--------+");
|
|
|
|
|
System.out.println("| Time(sec) | Time (UTC format) | TOTP | Mode |");
|
|
|
|
|
System.out.println("+---------------+-----------------------+--------+--------+");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < testTime.length; i++) {
|
|
|
|
|
String fmtTime = String.format("%1$-11s", testTime[i]);
|
|
|
|
|
String utcTime = df.format(new Date(testTime[i] * 1000));
|
|
|
|
|
|
|
|
|
|
String sha1Pin = testOTP1.getPinAt(testTime[i]);
|
|
|
|
|
String sha256Pin = testOTP256.getPinAt(testTime[i]);
|
|
|
|
|
String sha512Pin = testOTP512.getPinAt(testTime[i]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
|
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha1Pin + "| SHA1 |");
|
|
|
|
|
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha256Pin + "| SHA256 |");
|
|
|
|
|
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha512Pin + "| SHA512 |");
|
|
|
|
|
System.out.println("+---------------+-----------------------+--------+--------+");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedValues[i * 3 + 0], sha1Pin);
|
|
|
|
|
assertEquals(expectedValues[i * 3 + 1], sha256Pin);
|
|
|
|
|
assertEquals(expectedValues[i * 3 + 2], sha512Pin);
|
|
|
|
|
}
|
|
|
|
|
} catch (final Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
|
|
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 testOTP512 = (TOTP) OTPType.TOTP.instance(seed64, OTPAlgorithm.SHA512, 8, 0, 30, false);
|
|
|
|
|
|
|
|
|
|
long X = 30;
|
|
|
|
|
|
|
|
|
|
String steps = "0";
|
|
|
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
|
System.out.println("+---------------+-----------------------+--------+--------+");
|
|
|
|
|
System.out.println("| Time(sec) | Time (UTC format) | TOTP | Mode |");
|
|
|
|
|
System.out.println("+---------------+-----------------------+--------+--------+");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < testTime.length; i++) {
|
|
|
|
|
String fmtTime = String.format("%1$-11s", testTime[i]);
|
|
|
|
|
String utcTime = df.format(new Date(testTime[i] * 1000));
|
|
|
|
|
|
|
|
|
|
String sha1Pin = testOTP1.getPinAt(testTime[i]);
|
|
|
|
|
String sha256Pin = testOTP256.getPinAt(testTime[i]);
|
|
|
|
|
String sha512Pin = testOTP512.getPinAt(testTime[i]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(VERBOSE) {
|
|
|
|
|
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha1Pin + "| SHA1 |");
|
|
|
|
|
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha256Pin + "| SHA256 |");
|
|
|
|
|
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha512Pin + "| SHA512 |");
|
|
|
|
|
System.out.println("+---------------+-----------------------+--------+--------+");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedValues[i * 3 + 0], sha1Pin);
|
|
|
|
|
assertEquals(expectedValues[i * 3 + 1], sha256Pin);
|
|
|
|
|
assertEquals(expectedValues[i * 3 + 2], sha512Pin);
|
|
|
|
|
}
|
|
|
|
|
} catch (final Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @org.junit.jupiter.api.Test
|
|
|
|
|