Documentation
¶
Index ¶
- Variables
- type Algorithm
- type HOTP
- type Option
- type TOTP
- func (t *TOTP) Algorithm() Algorithm
- func (t *TOTP) Digits() int
- func (t *TOTP) Generate(secret []byte) (string, error)
- func (t *TOTP) GenerateWithTime(tm time.Time, secret []byte) (string, error)
- func (t *TOTP) GetURI(accountName, issuer string, secret []byte) (string, error)
- func (t *TOTP) Period() int64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedAlgorithm is returned when an unsupported algorithm is requested. ErrUnsupportedAlgorithm = errors.New("unsupported algorithm") )
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm int
Algorithm represents the hashing algorithm used in OTP generation.
type HOTP ¶ added in v0.2.0
type HOTP struct {
// contains filtered or unexported fields
}
HOTP is HMAC-based One-Time Password algorithm implementation.
Example ¶
package main
import (
"fmt"
"github.com/ghosind/go-otp"
)
func main() {
hotp := otp.NewHOTP()
secret := []byte("12345678901234567890")
code, _ := hotp.Generate(0, secret)
fmt.Println(code)
}
Output: 755224
func (*HOTP) Digits ¶ added in v0.2.0
Digits returns the number of digits in the generated HOTP. If not set, it defaults to 6.
type Option ¶
type Option func(*otpBuilder)
Option represents a configuration option for OTPs.
func WithAlgorithm ¶
WithAlgorithm sets the hashing algorithm for the OTP.
func WithDigits ¶
WithDigits sets the number of digits for the OTP.
func WithPeriod ¶
WithPeriod sets the time period in seconds for which a TOTP is valid. It configures the TOTP only.
type TOTP ¶
type TOTP struct {
// contains filtered or unexported fields
}
TOTP is Time-based One-Time Password algorithm implementation.
Example ¶
package main
import (
"fmt"
"time"
"github.com/ghosind/go-otp"
)
func main() {
totp := otp.NewTOTP()
secret := []byte("12345678901234567890")
code, _ := totp.GenerateWithTime(time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC), secret)
fmt.Println(code)
}
Output: 413931
func (*TOTP) Algorithm ¶
Algorithm returns the hashing algorithm used in TOTP. If not set, it defaults to HmacSha1.
func (*TOTP) Digits ¶
Digits returns the number of digits in the generated TOTP. If not set, it defaults to 6.
func (*TOTP) Generate ¶
Generate generates a TOTP using the current time and the provided secret key.
func (*TOTP) GenerateWithTime ¶
GenerateWithTime generates a TOTP for the given time and secret key.