Documentation
¶
Overview ¶
Package gotp is a package for generating and verifying one-time passwords.
It can be used to implement two-factor (2FA) or multi-factor (MFA) authentication methods anywhere that requires users to log in.
Open MFA standards are defined in RFC 4226 (HOTP: An HMAC-Based One-Time Password Algorithm) and in RFC 6238 (TOTP: Time-Based One-Time Password Algorithm). GOTP implements server-side support for both of these standards.
This is a fork of xlzd/gotp
Index ¶
Examples ¶
Constants ¶
const MaxOTPLength = 8
MaxOTPLength is the maximun character length that OTP can be set to in the library
Variables ¶
This section is empty.
Functions ¶
func DecodeBase32 ¶
DecodeBase32 decodes a base32 string and returns a byte array or error if it is not a valid base32 string
func EncodeBase32 ¶
EncodeBase32 encodes a byte array into a base32 string
Types ¶
type HOTP ¶
type HOTP struct {
// contains filtered or unexported fields
}
HOTP generates usage counter based OTPs
func NewHOTP ¶
NewHOTP returns a HOTP struct with the given secret and set defaults. The digit count is 6, hasher SHA1 and format is decimal output.
Example ¶
secret, err := DecodeBase32("4S62BZNFXXSZLCRO") if err != nil { panic(err) } otp, err := NewHOTP(secret) if err != nil { panic(err) } otpAt, err := otp.At(0) if err != nil { panic(err) } fmt.Printf("one-time password of counter 0 is: %v\n", otpAt) uri, err := otp.ProvisioningURI("demoAccountName", "issuerName", 1) if err != nil { panic(err) } fmt.Printf("uri: %s\n", uri) valid, err := otp.Verify("944181", 0) if err != nil { panic(err) } fmt.Printf("otp is valid: %v\n", valid)
Output: one-time password of counter 0 is: 944181 uri: otpauth://hotp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&counter=1&issuer=issuerName otp is valid: true
func (*HOTP) ProvisioningURI ¶
ProvisioningURI returns the provisioning URI for the OTP. This can then be encoded in a QR Code and used to provision an OTP app like Google Authenticator.
It can be given a human readable "accountName" and "issuerName", as well as an "initialCount" for the OTP generation.
See https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
type Hasher ¶
type Hasher struct { // HashName is unique identifier for this hashing implementation HashName string // Digest is a function that returns a `hash.Hash` when called Digest func() hash.Hash }
Hasher provides a custom hashing implementation for a OTP
type OTPOption ¶
type OTPOption func(*otpOptions) error
OTPOption configures OTPs
func FormatHex ¶
func FormatHex() OTPOption
FormatHex lets OTPs be returned in Hexadecimal format instead of Decimal format
func WithHasher ¶
WithHasher lets OTPs be generated using the given hasher
func WithInterval ¶
WithInterval lets TOTPs have the given interval for changing its values
func WithLength ¶
WithLength make generated OTPs have the given length
type TOTP ¶
type TOTP struct {
// contains filtered or unexported fields
}
TOTP generates time-based OTPs
func NewTOTP ¶
NewTOTP returns a TOTP struct with the given secret and set defaults. The digit count is 6, interval 30, hasher SHA1 and format is decimal output.
Example ¶
secret, err := DecodeBase32("4S62BZNFXXSZLCRO") if err != nil { panic(err) } otp, err := NewTOTP(secret) if err != nil { panic(err) } otpAt, err := otp.At(0) if err != nil { panic(err) } fmt.Printf("one-time password of timestamp 0 is: %v\n", otpAt) uri, err := otp.ProvisioningURI("demoAccountName", "issuerName") if err != nil { panic(err) } fmt.Printf("uri: %s\n", uri) valid, err := otp.Verify("179394", 1524485781) if err != nil { panic(err) } fmt.Printf("otp is valid: %v\n", valid)
Output: one-time password of timestamp 0 is: 944181 uri: otpauth://totp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&issuer=issuerName otp is valid: true
func (*TOTP) Now ¶
Now generates the current time-based OTP.
Example ¶
secret, err := DecodeBase32("4S62BZNFXXSZLCRO") if err != nil { panic(err) } otp, err := NewTOTP(secret) if err != nil { panic(err) } currentOTP, err := otp.Now() if err != nil { panic(err) } fmt.Printf("current one-time password is: %v\n", currentOTP)
Output:
func (*TOTP) NowWithExpiration ¶
NowWithExpiration generates the current time-based OTP and expiration time.
func (*TOTP) ProvisioningURI ¶
ProvisioningURI returns the provisioning URI for the TOTP. This can then be encoded in a QR Code and used to provision an OTP app like Google Authenticator.
It can be given a human readable "accountName" and "issuerName" for the TOTP generation.
See https://github.com/google/google-authenticator/wiki/Key-Uri-Format.