totp

package
v0.0.0-...-80f7e85 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package totp is used to generate and verify Timed One-Time Passwords.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("invalid")

ErrInvalid is returned when a parameter fails validation.

View Source
var ErrTokenFailed = errors.New("could not verify token")

ErrTokenFailed is returned when a TOTP fails verification.

Functions

This section is empty.

Types

type HashAlgorithm

type HashAlgorithm int

HashAlgorithm defines which hashing algorithm to use when generating a TOTP. The default for most apps is SHA-1.

const (
	SHA1 HashAlgorithm = iota
	SHA256
	SHA512
)

Hashing algorithms. The default for most apps is SHA-1.

type Params

type Params struct {
	Issuer        string
	AccountName   string
	HashAlgorithm HashAlgorithm
	SecretLength  uint
	Digits        uint8
	Period        time.Duration
	Lookback      uint
}

Params can configure optional parameters for new TOTP generation.

type TOTP

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

TOTP is used to generate and verify Timed One Time Password tokens.

func FromString

func FromString(uri string) (*TOTP, error)

FromString loads a URI-encoded TOTP message. This should be used when loading TOTP secrets from a QR Code.

func New

func New(params Params) (*TOTP, error)

New creates a new TOTP generator/verifier with a randomly generated secret.

func Unmarshal

func Unmarshal(encoded []byte) (*TOTP, error)

Unmarshal loads a proto-encoded TOTP message. This should be used when loading TOTP secrets from storage.

func (*TOTP) Generate

func (t *TOTP) Generate() (string, error)

Generate generates a new TOTP.

func (*TOTP) Marshal

func (t *TOTP) Marshal() ([]byte, error)

Marshal serializes the TOTP object in a protobuf format. This should be used for storing a TOTP secret.

func (*TOTP) Secret

func (t *TOTP) Secret() string

Secret outputs the secret in a base32-encoded format. This is useful for copy/paste into authenticator apps when a QR code is not available. It is highly recommended to use SHA-1 with this as many apps will default to SHA-1.

func (*TOTP) String

func (t *TOTP) String() string

String serializes the TOTP object in a URI format that is compatible with Google Authenticator.

func (*TOTP) Verify

func (t *TOTP) Verify(code string) error

Verify verifies a given TOTP.

Jump to

Keyboard shortcuts

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