kotp is a Kotlin/JVM library for generating and verifying One-Time Password (OTP) codes, suitable for 2FA (Two-Factor Authentication) implementations.
- HMAC-based OTP (HOTP): Generate and verify OTP codes based on a counter.
- Time-based OTP (TOTP): Generate and verify OTP codes based on time.
- Configurable: Define code length, time step and hashing algorithm
- Lightweight: Minimal dependencies and easy integration
Add the library to your project by including the following dependency:
dependencies {
implementation("dev.vicart:kotp:<version>")
}
<dependency>
<groupId>dev.vicart</groupId>
<artifactId>kotp</artifactId>
<version>...</version>
</dependency>
val totpGenerator = TOTPGenerator()
val code = totpGenerator.generateCode("base32-encoded key")
val totpVerifier = TOTPVerifier()
val verified = totpVerifier.verify("base32-encoded key", "OTP code")
Note: kotp automatically checks for previous and next time step on verification for error detection
val hotpGenerator = HOTPGenerator()
val code = hotpGenerator.generateCode("base32-encoded key", 0)
val hotpVerifier = HOTPVerifier()
val verified = hotpVerifier.verify("base32-encoded key", "OTP code", 0)
The library provides various options for customization:
codeLength
: Number of digits in the OTP (default: 6)period
: Duration (in seconds) for which a TOTP is valid (Default: 30 seconds)algorithm
: Hashing algorithm for OTP generation (Default: SHA1)