gotp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 10 Imported by: 0

README

G - Otp

G - Otp is a Golang client for Google Authenticator.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = config{

	Digits: DigitsSix,

	SecretSize: 16,

	Period: 30,

	Algorithm: AlgorithmSHA1,

	ValidChars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
}

Config is the configuration for the package. It can be changed to customize the package. The default configuration is:

SecretSize:    16,
Period:        30,
Algorithm:     AlgorithmSHA1,
Digits:        DigitsSix,
ValidChars:    "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",

Functions

func CreateSecret

func CreateSecret() string

CreateSecret returns a random secret key. The secret key is a string of characters from the set A-Z, 2-7. The default length of the secret key is 16 characters. This can be changed by setting Config.SecretSize. The default set of characters can be changed by setting Config.ValidChars.

func GetCode

func GetCode(secret string, time int64) string

GetCode returns the current code for the secret key. The code is valid for the current period. The period is 30 seconds by default. This can be changed by setting Config.Period.

func GetQRCode

func GetQRCode(config QrConfig) string

GetQRCode returns the URL of the QR code for the secret key. The QR code can be used to add the secret key to an authenticator app. The QR code is a Google Chart. The QR code is a PNG image.

func GetTime

func GetTime() int64

GetTime returns the current time in Unix time divided by the period. This is used to generate the counter for the HOTP algorithm. The period is 30 seconds by default. This can be changed by setting Config.Period. The Unix time is divided by the period to make it easier to test. This way, the counter can be set to any value.

func Verify

func Verify(config VerifyConfig) bool

Verify verifies the code against the secret key. It returns true if the code is valid, false otherwise.

Types

type Algorithm

type Algorithm int

Algorithm is the hash function used to generate the code.

const (
	// AlgorithmSHA1 is the hash function used to generate the code.
	AlgorithmSHA1 Algorithm = 1

	// AlgorithmSHA256 is the hash function used to generate the code.
	AlgorithmSHA256 Algorithm = 2

	// AlgorithmSHA512 is the hash function used to generate the code.
	AlgorithmSHA512 Algorithm = 3
)

func (Algorithm) Hash

func (a Algorithm) Hash() func() hash.Hash

Hash returns the hash function.

func (Algorithm) String

func (a Algorithm) String() string

String returns the string representation of the hash function.

type Digits

type Digits int

Digits is the number of digits in the code. It can be 6 or 8. The default is 6.

const (
	// DigitsSix is the number of digits in the code.
	DigitsSix Digits = 6

	// DigitsEight is the number of digits in the code.
	DigitsEight Digits = 8
)

func (Digits) Length

func (d Digits) Length() int

Length returns the number of digits.

func (Digits) String

func (d Digits) String() string

String returns the string representation of the number of digits.

type QrConfig

type QrConfig struct {
	// Secret is the secret key.
	Secret string

	// Issuer is the name of the issuer. This is optional. If set, it will be displayed on the authenticator app. For example, "Google".
	Issuer string

	// AccountName is the name of the account. This is required. For example, "John Doe".
	// This will be displayed on the authenticator app.
	// The final display will be "Issuer (AccountName)" or "AccountName" if Issuer is not set.
	AccountName string
}

QrConfig is the configuration for the GetQRCode function.

type VerifyConfig

type VerifyConfig struct {
	// Secret is the secret key.
	// It must be a string of valid characters.
	Secret string
	// Code is the code to verify.
	// It must be a string of digits.
	Code string
	// Window is the number of codes to check before and after the current code.
	// If Window is 0, only the current code is checked.
	Window int
}

VerifyConfig is the configuration for the Verify function.

Jump to

Keyboard shortcuts

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