Code Monkey home page Code Monkey logo

totp's Introduction

TOTP (Time-based One Time Password)

This package provides a convenient way of generarting Time-based One-Time Passwords in accordance with IETF's RFC6238.

Hash-based One-Time Password Algorithm(HOTP)uses a counter as a moving factor while generating the OTP. This counter is hashed using an HMAC hashing algorithms (HAMC-SHA1, HMAC-SHA256 or HMAC-512). TOTP builds on on HOTP by deriving the moving factor from UNIX time instead of a counter.

TOTP can be used to create OTPs containing more than 6 digits. Users can choose between the HMAC-SHA1, HMAC-SHA256 and HMAC-SHA512 for generating TOTPs.

Example

package main

import (
 "github.com/kelsier27/totp"
 "fmt"
)

func main() {
  var sharedSecret = "3132333435363738393031323334353637383930"
  otpgen := totp.NewDefaultTOTP(sharedSecret)
  totp := otpgen.GenerateTOTP("1970-01-01T00:00:59Z")
  fmt.Println(totp) // output - 94287082
}

Install

TOTP can be installed using go get

go get github.com/kelsier27/totp

Usage

  1. Obtain TOTPConfig object

Before generating the OTP, we need to obtain a TOTPConfig object. The default objects generate a 8-digit TOTP using T0 as Epoch time 1970-01-01T00:00:00Z and Time Step X as 30s. To get the default TOTPConfig, use one of the following functions depending on the choice of HMAC algorithm

  • NewDefaultSHA1
  • NewDefaultSHA256
  • NewDefaultSHA512
  1. Generate TOTP

Once we obtain a config object, we can call the GenerateTOTP method. This method takes a time string as the input and returns a TOTP string.

totp's People

Contributors

ekamwalia avatar giosal avatar

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.