h2otp

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TOTPPeriodDefault is the default TOTP period
	TOTPPeriodDefault uint64 = 30
	// OTPDigitsDefault is the default OTP length
	OTPDigitsDefault = 6
)
View Source
const (
	AlgSHA1   = "SHA1"
	AlgSHA256 = "SHA256"
	AlgSHA512 = "SHA512"
)

Hash algorithms

Variables

View Source
var (
	// ErrInvalidOpt is returned when an invalid opt is passed to otp
	ErrInvalidOpt errInvalidOpt
	// ErrOptUnsupported is returned when an otp opt is unsupported
	ErrOptUnsupported errOptUnsupported
	// ErrParamInvalid is returned when an otp param string is invalid
	ErrParamInvalid errParamInvalid
)
View Source
var DefaultHashes = otpHashes{
	AlgSHA1:   crypto.SHA1.New,
	AlgSHA256: crypto.SHA256.New,
	AlgSHA512: crypto.SHA512.New,
}

DefaultHashes are the hashes defined by RFC6238

Functions

func GenerateRandomCode

func GenerateRandomCode(digits int) (string, error)

GenerateRandomCode generates a random code with a specified length

func HOTP

func HOTP(secret []byte, counter uint64, alg HashConstructor, digits int) (string, error)

HOTP implements RFC4226

func TOTP

func TOTP(secret []byte, t uint64, opts TOTPOpts) (string, error)

TOTP implements RFC6238

func TOTPGenerateSecret

func TOTPGenerateSecret(secretLength int, opts TOTPURI) (string, string, error)

TOTPGenerateSecret generates an otp secret

func TOTPNow

func TOTPNow(secret []byte, opts TOTPOpts) (string, error)

TOTPNow returns the TOTP now

func TOTPVerify

func TOTPVerify(params string, code string, hashes Hashes) (bool, error)

TOTPVerify verifies an otp

Types

type HashConstructor

type HashConstructor = func() hash.Hash

HashConstructor constructs a new hash

type Hashes

type Hashes interface {
	Get(id string) (HashConstructor, bool)
}

Hashes are a map of valid hashes

type TOTPConfig

type TOTPConfig struct {
	Secret []byte
	Alg    string
	Digits int
	Period uint64
	Leeway uint64
}

TOTPConfig are opts for TOTP

func (TOTPConfig) String

func (c TOTPConfig) String() string

type TOTPOpts

type TOTPOpts struct {
	Alg    HashConstructor
	Digits int
	Period uint64
}

TOTPOpts are opts for TOTP

type TOTPURI

type TOTPURI struct {
	TOTPConfig
	Issuer      string
	AccountName string
}

TOTPURI are opts for OTP apps

func (TOTPURI) String

func (c TOTPURI) String() string

Jump to

Keyboard shortcuts

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