totp

package module
v0.0.0-...-66805c3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2019 License: MIT Imports: 11 Imported by: 0

README

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.

Documentation

Overview

Package totp generates a Timed One Time Password conforming to RFC6238

Index

Constants

View Source
const (
	DefaultT0         = "1970-01-01T00:00:00Z"
	DefaultX          = 30
	DefaultCodeDigits = 10

	CryptoSHA1   = "HMACSHA1"
	CryptoSHA256 = "HMACSHA256"
	CryptoSHA512 = "HMACSHA512"
)

Variables

View Source
var (
	DIGIT_POWERS = []int64{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000}
)

Functions

This section is empty.

Types

type TOTPConfig

type TOTPConfig struct {
	// Epoc time in standard format
	// eg: "1970-01-01T00:00:00Z"
	T0 string

	// Time step
	// eg 30.0
	X int64

	// shared secret K
	SecretK string

	// number of digits in OTP
	CodeDigits int

	// HMAC algorithm
	// enum{'HMACSHA1', 'HMACSHA256', 'HMACSHA512'}
	Crypto string
}

func DefaultConfig

func DefaultConfig(seed string) *TOTPConfig

Returns TOTP config object with defaults

func NewDefaultSHA1

func NewDefaultSHA1(seed string) *TOTPConfig

* Returns a TOTP config object that uses SHA1 for HMAC hashing and default values for `T0`, `X`, and `CodeDigits`.

The secret key still needs supplied by the user.

func NewDefaultSHA256

func NewDefaultSHA256(seed string) *TOTPConfig

* Returns a TOTP config object that uses SHA256 for HMAC hashing and default values for `T0`, `X`, and `CodeDigits`.

The secret key still needs supplied by the user.

func NewDefaultSHA512

func NewDefaultSHA512(seed string) *TOTPConfig

* Returns a TOTP config object that uses SHA512 for HMAC hashing and default values for `T0`, `X`, and `CodeDigits`.

The secret key still needs supplied by the user.

func (*TOTPConfig) GenerateTOTP

func (otpConf *TOTPConfig) GenerateTOTP(date string) (string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL