otp

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 16 Imported by: 0

README

Go OTP

Go Reference

go-otp is an implementation of RFC 4226, and RFC 6238 for Go. It is used internally by BerryByte Limited. No external dependencies, just your plain ol' Go stdlib.

Installing

go get -u github.com/pixec/go-otp

License

go-otp is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidCode = errors.New("go-otp: invalid code given")

Functions

This section is empty.

Types

type Digits

type Digits uint8
const (
	DigitsSix   Digits = 6
	DigitsEight Digits = 8
)

func (Digits) Int

func (d Digits) Int() int

func (Digits) String

func (d Digits) String() string

type HOTP

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

func NewHOTP

func NewHOTP(opts HOTPOptions) (*HOTP, error)

NewHOTP returns a new HOTP.

func (*HOTP) Base32Secret

func (h *HOTP) Base32Secret() string

Base32Secret returns the HOTP secret encoded in base32.

func (*HOTP) Counter

func (h *HOTP) Counter() uint64

Counter returns the HOTP counter.

func (*HOTP) Digits

func (h *HOTP) Digits() Digits

Digits returns the HOTP digits.

func (*HOTP) Generate

func (h *HOTP) Generate() (string, error)

Generate returns cryptographically generated HOTP code.

func (*HOTP) Hash

func (h *HOTP) Hash() Hash

Hash returns the HOTP hash algorithm.

func (*HOTP) Secret

func (h *HOTP) Secret() []byte

Secret returns the HOTP secret.

func (*HOTP) SetCounter

func (h *HOTP) SetCounter(counter uint64)

SetCounter sets the HOTP counter to given counter.

func (*HOTP) SetDigits

func (h *HOTP) SetDigits(digits Digits)

SetDigits sets the HOTP digits.

func (*HOTP) SetHash

func (h *HOTP) SetHash(hash Hash)

SetHash sets the hash algorithm for the HOTP.

func (*HOTP) SetSecret

func (h *HOTP) SetSecret(secret []byte)

SetSecret sets the secret for the HOTP.

func (*HOTP) Validate

func (h *HOTP) Validate(code string) error

Validate validates given HOTP code.

type HOTPOptions

type HOTPOptions struct {
	Secret  []byte // Secret between client and server. K in RFC 4226.
	Counter uint64 // The moving factor. C in RFC 4226.
	Digits  Digits // Number of digits in an HOTP code. Digit in RFC 4226.
	Hash    Hash   // Algorithm for the HOTP.
}

type Hash

type Hash uint8
const (
	HashSHA1 Hash = iota + 1
	HashSHA256
	HashSHA512
)

func (Hash) Hash

func (h Hash) Hash() hash.Hash

Hash returns hash.Hash, computing checksum for hash.

func (Hash) Size

func (h Hash) Size() int

Size returns the size of a checksum for hash in bytes.

func (Hash) String

func (h Hash) String() string

String returns the representation of hash in string.

type TOTP

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

func NewTOTP

func NewTOTP(opts TOTPOptions) (*TOTP, error)

NewTOTP returns a new TOTP.

func (*TOTP) Base32Secret

func (t *TOTP) Base32Secret() string

Base32Secret returns the TOTP secret encoded in base32.

func (*TOTP) Digits

func (t *TOTP) Digits() Digits

Digits returns the TOTP digits.

func (*TOTP) Generate

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

Generate returns cryptographically generated TOTP code.

func (*TOTP) Hash

func (t *TOTP) Hash() Hash

Hash returns the TOTP hash algorithm.

func (*TOTP) Period

func (t *TOTP) Period() uint64

Digits returns the TOTP digits.

func (*TOTP) Secret

func (t *TOTP) Secret() []byte

Secret returns the TOTP secret.

func (*TOTP) SetDigits

func (t *TOTP) SetDigits(digits Digits)

SetDigits sets the TOTP digits.

func (*TOTP) SetHash

func (t *TOTP) SetHash(hash Hash)

SetHash sets the hash algorithm for the TOTP.

func (*TOTP) SetPeriod

func (t *TOTP) SetPeriod(period uint64)

SetDigits sets the TOTP period.

func (*TOTP) SetSecret

func (t *TOTP) SetSecret(secret []byte)

SetSecret sets the secret for the TOTP.

func (*TOTP) SetTime

func (t *TOTP) SetTime(tm time.Time)

SetTime sets the TOTP time.

func (*TOTP) SetTimeStart

func (t *TOTP) SetTimeStart(tm time.Time)

SetTimeStart sets the TOTP time start.

func (*TOTP) Time

func (t *TOTP) Time() time.Time

Time returns the TOTP time.

func (*TOTP) TimeStart

func (t *TOTP) TimeStart() time.Time

Time returns the TOTP time start.

func (*TOTP) URL

func (t *TOTP) URL(issuer, accountName string) *url.URL

URL returns net/url.URL representation for the TOTP.

func (*TOTP) Validate

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

Validate validates given TOTP code.

type TOTPOptions

type TOTPOptions struct {
	Secret    []byte    // Secret between client and server. K in RFC 4226.
	Time      time.Time // Current time in RFC 6238.
	TimeStart time.Time // Time to start counting time steps. T0 in RFC 6238.
	Digits    Digits    // Number of digits in a TOTP code. Digit in RFC 4226.
	Period    uint64    // Time step in seconds, X in RFC 6238, 30 seconds is recommended.
	Hash      Hash      // Algorithm for the TOTP, SHA256 is recommended.
}

Jump to

Keyboard shortcuts

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