Added requirements checks

This commit is contained in:
TheArrayser 2023-06-28 17:01:41 +02:00
parent 806726d109
commit fb2923f761
2 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package com.cringe_studios.cringe_authenticator_library;
import java.security.InvalidKeyException;
import com.cringe_studios.cringe_authenticator_library.impl.Base32;
public abstract class OTP {
@ -45,7 +47,8 @@ public abstract class OTP {
* @param checksum OPTIONAL: appends a checksum digit to the end of the string
* @return returns a String containing the OTP Digits and the optional checksum at the end
*/
public static OTP createNewOTP(OTPType type, String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) {
public static OTP createNewOTP(OTPType type, String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) throws OTPException{
if(type == null) return null;
return type.instance(secret, algorithm, digits, counter, periodInSeconds, checksum);
}

View File

@ -1,5 +1,7 @@
package com.cringe_studios.cringe_authenticator_library;
import com.cringe_studios.cringe_authenticator_library.impl.Base32;
public enum OTPType {
HOTP("HMAC-based One-Time Password"),
TOTP("Time-based One-Time Password");
@ -15,6 +17,16 @@ public enum OTPType {
}
public OTP instance(String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) {
if(/*type == null ||*/ secret == null /*|| counter < 0*/) {
return null;
}
try {
Base32.decode(secret);
}catch (IllegalArgumentException e) {
throw new OTPException("Your secret is invalid! Please rescan the code!", e);
}
switch(this) {
case HOTP:
return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);