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 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 (*HOTP) GenerateCode ¶
GenerateCode for the given counter and secret.
func (*HOTP) GenerateURL ¶
GenerateURL for the account for a given secret.
type HOTPConfig ¶ added in v0.2.0
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 ¶
ParseKeyFromURL creates a new Key from the HOTP or TOTP URL. See: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
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 (*TOTP) GenerateCode ¶
GenerateCode for the given counter and secret.
func (*TOTP) GenerateURL ¶
GenerateURL for the account for a given secret.
Click to show internal directories.
Click to hide internal directories.