totp

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 12 Imported by: 0

README

TOTP

Go implementation of Time-Based One-Time Password Algorithm defined in RFC6238.

The key URI format used in this library is introduced in https://github.com/google/google-authenticator/wiki/Key-Uri-Format.

Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/tmsick/totp"
)

func main() {
	// Generate virtual token device based on parameters specified in URI
	uri := "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example"
	token, err := totp.NewToken(uri)
	if err != nil {
		log.Fatal(err)
	}

	// Get current TOTP
	now := time.Now()
	totpstring := token.Generate(now)
	fmt.Println(totpstring)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Token

type Token struct {
	// contains filtered or unexported fields
}

A Token represents a virtual TOTP token that generates a Time-Based One-Time Password defined in RFC 6238.

func NewToken

func NewToken(uri string) (*Token, error)

NewToken returns a new virtual TOTP token with parameters specified by a Key URI. The Key URI format is defined in https://github.com/google/google-authenticator/wiki/Key-Uri-Format.

Users of this library have to specify at least `secret` in query parameter as defined in the spec. Other parameters have default values like below:

  • issuer = ""
  • algorithm = "SHA1" (Other available options are "SHA256" and "SHA512")
  • digits = 6
  • period = 30

`digits` and `period` have a limited range as below:

  • 6 <= digits <= 10
  • 1 <= period <= 90

NewToken doesn't panic and merely returns an error should there be any violation in a Key URI passed.

func (*Token) Algorithm

func (t *Token) Algorithm() string

Algorithm returns the hash function name used to generate TOTPs. It should return "SHA1", "SHA256", or "SHA512".

func (*Token) Digits

func (t *Token) Digits() int

Digits returns the number of digits OTPs have.

func (*Token) Generate

func (t *Token) Generate(m time.Time) string

Generate returns a TOTP value calculated with the token's parameters and a specified time.

func (*Token) Issuer

func (t *Token) Issuer() string

Issuer returns the issuer value of the Key URI.

func (*Token) Label

func (t *Token) Label() string

Label returns the label part of the Key URI without leading or trailing slashes.

func (*Token) Period

func (t *Token) Period() int

Period returns the time duration in seconds a TOTP lives.

Jump to

Keyboard shortcuts

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