twofa

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2022 License: BSD-3-Clause Imports: 12 Imported by: 0

README

Two-FA

A two-factor authentication(2FA) based on HOTP & TOTP written in Go.

Features

  • Based on HOTP/TOTP algorithm
  • Get code by encoded key
  • Encode/Decode key
  • Encode key for iOS
  • Encode key with hash
  • Gen random key
  • Print QR for Google Authenticator in terminal

Install

go get github.com/jovijovi/two-fa

Example Usage

Roadmap

  • Documents

License

This project is licensed under the BSD 3-Clause License.

Documentation

Index

Constants

View Source
const (
	CodeDigits6 = 6
	CodeDigits7 = 7
	CodeDigits8 = 8

	// TotpFormatter TOTP formatter
	// Example:
	// otpauth://totp/NAME?secret=KEY
	TotpFormatter = "otpauth://totp/%s?secret=%s"
)
View Source
const (
	BrightBlack = "\033[48;5;0m  \033[0m"
	BrightWhite = "\033[48;5;7m  \033[0m"
)

Variables

This section is empty.

Functions

func DecodeKey

func DecodeKey(key string) ([]byte, error)

DecodeKey returns decoded key

func EncodeKey

func EncodeKey(raw string, opt ...OptionFunc) string

EncodeKey returns encoded key

func EncodeKeyForIOS

func EncodeKeyForIOS(raw string, opt ...OptionFunc) string

EncodeKeyForIOS returns encoded key for iOS

func GenKey added in v0.3.1

func GenKey(opt ...OptionFunc) string

GenKey returns random encoded key

func GenKeyForIOS added in v0.3.1

func GenKeyForIOS(opt ...OptionFunc) string

GenKeyForIOS returns random encoded key for iOS

func GetCode

func GetCode(key string) (string, error)

GetCode returns time based code by encode key

func GetCodeByRaw

func GetCodeByRaw(key Key) (uint32, error)

GetCodeByRaw returns time based code by raw key

func PrintQR added in v0.3.1

func PrintQR(content string) error

Types

type HashFunc added in v0.3.1

type HashFunc struct {
	Provider HashProvider
}

func (*HashFunc) Hash added in v0.3.1

func (h *HashFunc) Hash(msg []byte) ([]byte, error)

type HashProvider added in v0.3.1

type HashProvider func() hash.Hash

type IHashFunc added in v0.3.1

type IHashFunc interface {
	Hash(msg []byte) ([]byte, error)
}

func DefaultHashFunc added in v0.3.1

func DefaultHashFunc() IHashFunc

type Key

type Key struct {
	Raw    []byte
	Digits uint32
	Offset uint32 // counter offset
}

type OptionFunc added in v0.3.1

type OptionFunc func(opts *Options)

OptionFunc used to initialise

func WithDefaultHashFunc added in v0.3.1

func WithDefaultHashFunc() OptionFunc

WithDefaultHashFunc option to configure default hash function (sha1)

func WithHashFunc added in v0.3.1

func WithHashFunc(hashFunc IHashFunc) OptionFunc

WithHashFunc option to configure hash function

func WithQR added in v0.3.1

func WithQR(name string) OptionFunc

WithQR option to configure QR

type Options added in v0.3.1

type Options struct {
	// HashFunc interface
	HashFunc IHashFunc

	// With hash
	WithHash bool

	// Key name
	KeyName string

	// With QR
	WithQR bool

	// Options for implementations of the interface can be stored in a context
	Context context.Context
}

Options for func

func NewOptions added in v0.3.1

func NewOptions(optionFunc ...OptionFunc) Options

NewOptions new options

Jump to

Keyboard shortcuts

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