Skip to content

Java library with TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) implementations

License

Notifications You must be signed in to change notification settings

M3DZIK/java-otp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

One-time password generator

A simple and easy-to-use Java library with TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) implementations.

Getting started

First, add the library as a dependency to your Maven project.

<dependency>
    <groupId>dev.medzik</groupId>
    <artifactId>otp</artifactId>
    <version>1.0.1</version>
</dependency>

Usage

Parse OTPAuth URI

import dev.medzik.otp.OTPParameters;

String uri = "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example&algorithm=SHA512&digits=8";

OTPParameters params = OTPParameters.parseUrl(uri);

HOTP (HMAC-based One-Time Passwords)

import dev.medzik.otp.HOTPGenerator;

OTPParameters params = OTPParameters.builder()
        .type(OTPType.HOTP)
        .secret(new OTPParameters.Secret("secret"))
        // more options are available
        .build();

long counter = 1;

String code = HOTPGenerator.generate(params, counter);
boolean valid = HOTPGenerator.verify(params, code, counter);

TOTP (Time-based One-Time Passwords)

import dev.medzik.otp.TOTPGenerator;

OTPParameters params = OTPParameters.builder()
        .type(OTPType.TOTP)
        .secret(new OTPParameters.Secret("secret"))
        // more options are available
        .build();

String code = TOTPGenerator.now(params);
boolean valid = TOTPGenerator.verify(params, code);

About

Java library with TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) implementations

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages