Giter Site home page Giter Site logo

basic-java-otp's Introduction

Basic OTP Generator/Validator

A simple One-Time-Password generator and validator. An OTP is typically used in two-factor authentication, where the password is sent to a user via known email or phone number, which is then entered back on a site and validated, confirming the identity of the user.

OTPGenerator generates a random numeric password and corresponding HMAC using a secret key. Additional parameters may be added while generating HMAC. The password is sent to a secondary device considered 'pre-authenticated' and the HMAC is sent to the device requesting authentication. During the validation step, the user provided password and HMAC from earlier step is passed to the OTP Engine for validation. A wrong password results in mismatch of HMAC and therefore considered invalid.

Usage Guide

Steps to Generate a One-Time-Password

1. Create an instance of OTPEngine, passing in a secret key
OTPEngine engine = OTPEngine.getInstance(secretKey);
  1. Optionally, add some extra parameters to make the password/hmac combination unique to the user/instance

    String params = new String[] { "CUSTOMERID-12345" };

  2. Generate the password by calling the 'generatePasswordWithHmac' method on the OTP engine and passing in the extra parameters

    OTP otp = engine.generatePasswordWithHmac(params);

  3. The returned object contains two properties
    password - contains the generated random numeric password. The password is sent via secure means to the user's email or phone
    hmac - Hashed message authentication code associated with this password. HMAC value is returned to the front-end requesting the OTP and entered back along with the password during the validation step.

Steps to Validate One-Time-Password

1. Create an instance of OTPEngine, passing in the same secret key used while generating the password. If the key is different from the one used for generating the password, it cannot be validated!
OTPEngine engine = OTPEngine.getInstance(key);
  1. Validate the password using the 'validatePasswordWithHmac' method on the engine, passing in the password and hmac received from the front-end/user interface.

    boolean isValid = engine.validatePasswordWithHmac(new OTP(password, hmac), params); if(isValid){ //hooray!! } else { //fail!! }

  2. By default, generated password is valid for 10-15 minutes from the time it is generated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.