totp

package module
v0.0.0-...-0c3b638 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 9 Imported by: 1

README

totp

A TOTP Client and Server inspired by this hacker news post about how simple it is to write a TOTP client in Go. This was a great example of how to take the technical document RFC6238 and implement it. There were a few things about the post that could improved upon for fun practice.

  1. Make a Server that could produce a new secret and then subsequently verify the TOTP.
  2. Make the TOTP implementation a library so that it could be called from both Client and Server.
  3. Make the code testable.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(secret string, t0, x int64, d uint32, currentTimeFn func() int64) (code uint32, timeRemaining uint64, err error)

Generate a TOTP value and the number of seconds remaining that the code is valid for t0 is the Unix time to start counting time steps (default value is 0, i.e., the Unix epoch) and is also a system parameter x represents the time step size in seconds (default value x = 30 seconds) and is a system parameter d represents the number of digits the TOTP code will be currentTimeFn is a function that should return the current unix time from the UTC time zone

Types

type Generator

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

Generator is a type that simplifies generation of TOTP codes by allowing configuration to be saved to the generator object.

func NewGenerator

func NewGenerator(opts ...Option) *Generator

func (Generator) Generate

func (g Generator) Generate(secret string) (uint32, uint64, error)

type Option

type Option func(*Generator)

func WithCurrentTimeFn

func WithCurrentTimeFn(fn func() int64) Option

WithCurrentTimeFn sets the currentTimeFn to the specified function. Defaults to a function that returns time.Now().UTC().Unix()

func WithD

func WithD(d uint32) Option

WithD Option sets d to the specified value. Default value: 6 d represents the number of digits the OTP should be

func WithT0

func WithT0(t0 int64) Option

WithT0 Option sets t0 to the specified value. Default value: 0 t0 is the Unix time to start counting time steps

func WithX

func WithX(x int64) Option

WithX Option sets x to the specified value. Value must be greater than 0. Default value: 30 x represents the time step size in seconds

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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