public final class Otp
- Object
- Otp
Counter-based (HOTP, RFC 4226) and time-based (TOTP, RFC 6238) one-time password generators. Compatible with any standard authenticator app (Google Authenticator, Microsoft Authenticator, 1Password, etc.).
Generate a 6-digit Google-Authenticator-compatible code
byte[] secret = Base32.decode("JBSWY3DPEHPK3PXP"); // shared secret
String code = Otp.totp(secret); // default 6 digits, 30 second step,
// SHA-1, current time
Verify a code (allowing +/-1 step of clock skew)
boolean ok = Otp.verifyTotp(secret, userInput, 1);
Methods
Inherited methods
Method details
hotp
public static String hotp(byte[] secret, long counter, int digits)Parameters
secretbyte[]- the shared secret
counterlong- the moving factor – caller is responsible for incrementing it after every successful authentication
digitsint- number of decimal digits in the output (typically 6, may be 6, 7 or 8)
hotp
public static String hotp(byte[] secret, long counter, int digits, String hashAlgorithm)algorithm parameter in its provisioning URI.totp
public static String totp(byte[] secret)totp
public static String totp(byte[] secret, int digits, int stepSeconds)totp
public static String totp(byte[] secret, long currentTimeMillis, int stepSeconds, int digits, String hashAlgorithm)Parameters
secretbyte[]- shared secret
currentTimeMillislong- timestamp to derive the code from
stepSecondsint- window size – 30 in the vast majority of deployments
digitsint- number of decimal digits in the output (typically 6 or 8)
hashAlgorithmString- hash to use – almost always
Hash.SHA1
verifyTotp
public static boolean verifyTotp(byte[] secret, String code, int tolerance)tolerance steps of clock skew on either
side of now (so a tolerance of 1 will accept the previous, current
and next code).otpauthUri
public static String otpauthUri(String issuer, String accountName, byte[] secret, int digits, int stepSeconds, String hashAlgorithm)Builds the canonical otpauth://totp/... URI that authenticator apps
(Google Authenticator, Microsoft Authenticator, 1Password, Authy, …)
consume when the user scans a QR code on your enrolment screen. The
format is documented at
https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
Render the returned string as a QR code (server-side render, or a
QR-generation cn1lib) and show it to the user; they scan it, the
authenticator stores secret against the issuer:accountName label,
and from then on it produces six-digit codes that match
[#totp(byte[])] on your side using the same secret.
Parameters
issuerString- the human-readable service name shown in the authenticator
(“Acme Bank”). Must not contain a
:. accountNameString- the user’s identifier within your service
(“alice@example.com”). Must not contain a
:. secretbyte[]- shared secret (the bytes you also pass to [#totp(byte[])]) – encoded as Base32 in the URI per the spec.
digitsint- number of digits in each code (typically 6).
stepSecondsint- time-step size, typically 30.
hashAlgorithmString- hash, typically
Hash.SHA1for authenticator compatibility. SHA-256 and SHA-512 are accepted but not all authenticator apps support them.
otpauthUri
public static String otpauthUri(String issuer, String accountName, byte[] secret)verifyTotp
public static boolean verifyTotp(byte[] secret, String code, int tolerance, long currentTimeMillis, int stepSeconds, int digits, String hashAlgorithm)