otp

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 15 Imported by: 0

README

otp

build-img pkg-img reportcard-img coverage-img version-img

One time password for Go.

Features

  • Simple API.
  • Dependency-free.
  • Clean and tested code.
  • HOTP RFC 4226.
  • TOTP RFC 6238.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/otp

Example

secretInBase32 := "JBSWY3DPEHPK3PXP"

algo := otp.AlgorithmSHA1
digits := otp.Digits(10)
issuer := "cristalhq"

hotp, err := otp.NewHOTP(algo, digits, issuer)
checkErr(err)

code, err := hotp.GenerateCode(42, secretInBase32)
checkErr(err)

fmt.Println(code)

err = hotp.Validate(code, 42, secretInBase32)
checkErr(err)

// Output:
// 0979090604

Also see examples: examples_test.go.

Documentation

See these docs.

License

MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedAlgorithm = errors.New("unsupported algorithm")
	ErrNoDigits             = errors.New("required digits not set")
	ErrEmptyIssuer          = errors.New("empty issuer")
	ErrPeriodNotValid       = errors.New("period is not valid")
	ErrSkewNotValid         = errors.New("skew is not valid")
	ErrCodeLengthMismatch   = errors.New("code length mismatch")
	ErrCodeIsNotValid       = errors.New("code is not valid")
	ErrEncodingNotValid     = errors.New("encoding is not valid")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm uint

Algorithm represents the hashing function to use for OTP.

const (
	AlgorithmUnknown Algorithm = 0
	AlgorithmSHA1    Algorithm = 1
	AlgorithmSHA256  Algorithm = 2
	AlgorithmSHA512  Algorithm = 3
)

func (Algorithm) Hash

func (a Algorithm) Hash() hash.Hash

func (Algorithm) String

func (a Algorithm) String() string

type HOTP

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

HOTP represents HOTP codes generator and validator.

Example
package main

import (
	"fmt"

	"github.com/cristalhq/otp"
)

func main() {
	hotp, err := otp.NewHOTP(otp.HOTPConfig{
		Algo:   otp.AlgorithmSHA1,
		Digits: 10,
		Issuer: "cristalhq",
	})
	checkErr(err)

	secretInBase32 := "JBSWY3DPEHPK3PXP"
	code, err := hotp.GenerateCode(42, secretInBase32)
	checkErr(err)

	fmt.Println(code)

	err = hotp.Validate(code, 42, secretInBase32)
	checkErr(err)

}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
Output:

0979090604

func NewHOTP

func NewHOTP(cfg HOTPConfig) (*HOTP, error)

NewHOTP creates new HOTP.

func (*HOTP) GenerateCode

func (h *HOTP) GenerateCode(counter uint64, secret string) (string, error)

GenerateCode for the given counter and secret.

func (*HOTP) GenerateURL

func (h *HOTP) GenerateURL(account string, secret []byte) string

GenerateURL for the account for a given secret.

func (*HOTP) Validate

func (h *HOTP) Validate(passcode string, counter uint64, secret string) error

Validate the given passcode, counter and secret.

type HOTPConfig added in v0.2.0

type HOTPConfig struct {
	Algo   Algorithm
	Digits uint
	Issuer string
}

func (HOTPConfig) Validate added in v0.2.0

func (cfg HOTPConfig) Validate() error

type Key

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

Key represents an HTOP or TOTP key.

func ParseKeyFromURL

func ParseKeyFromURL(s string) (*Key, error)

ParseKeyFromURL creates a new Key from the HOTP or TOTP URL. See: https://github.com/google/google-authenticator/wiki/Key-Uri-Format

func (*Key) Account

func (k *Key) Account() string

Account returns the name of the user's account.

func (*Key) Issuer

func (k *Key) Issuer() string

Issuer returns the name of the issuing organization.

func (*Key) Period

func (k *Key) Period() uint64

Period returns a tiny int representing the rotation time in seconds.

func (*Key) Secret

func (k *Key) Secret() string

Secret returns the opaque secret for this Key.

func (*Key) String

func (k *Key) String() string

func (*Key) Type

func (k *Key) Type() string

Type returns "hotp" or "totp".

type TOTP

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

TOTP represents TOTP codes generator and validator.

Example
package main

import (
	"fmt"
	"time"

	"github.com/cristalhq/otp"
)

func main() {
	totp, err := otp.NewTOTP(otp.TOTPConfig{
		Algo:   otp.AlgorithmSHA1,
		Digits: 10,
		Issuer: "cristalhq",
		Period: 30,
		Skew:   2,
	})
	checkErr(err)

	secretInBase32 := "JBSWY3DPEHPK3PXP"
	at := time.Date(2023, 11, 26, 12, 15, 18, 0, time.UTC)

	code, err := totp.GenerateCode(secretInBase32, at)
	checkErr(err)

	fmt.Println(code)

	err = totp.Validate(code, at, secretInBase32)
	checkErr(err)

}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
Output:

0462778229

func NewTOTP

func NewTOTP(cfg TOTPConfig) (*TOTP, error)

NewTOTP creates new TOTP.

func (*TOTP) GenerateCode

func (t *TOTP) GenerateCode(secret string, at time.Time) (string, error)

GenerateCode for the given counter and secret.

func (*TOTP) GenerateURL

func (t *TOTP) GenerateURL(account string, secret []byte) string

GenerateURL for the account for a given secret.

func (*TOTP) Validate

func (t *TOTP) Validate(passcode string, at time.Time, secret string) error

Validate the given passcode, time and secret.

type TOTPConfig added in v0.2.0

type TOTPConfig struct {
	Algo   Algorithm
	Digits uint
	Issuer string
	Period uint
	Skew   uint
}

func (TOTPConfig) Validate added in v0.2.0

func (cfg TOTPConfig) Validate() error

Jump to

Keyboard shortcuts

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