-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotp.go
49 lines (43 loc) · 1.33 KB
/
hotp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gotp
// HOTP generates usage counter based OTPs
type HOTP struct {
*otp
}
// NewHOTP returns a HOTP struct with the given secret and set defaults.
// The digit count is 6, hasher SHA1 and format is decimal output.
func NewHOTP(secret []byte, opt ...OTPOption) (*HOTP, error) {
otp, err := newOTP(secret, opt...)
if err != nil {
return nil, err
}
return &HOTP{otp: otp}, nil
}
// At generates the OTP for the given `count` offset.
func (h *HOTP) At(count int) (string, error) {
return h.generateOTP(count)
}
// Verify verifies if a given OTP is valid at a given `count` offset
func (h *HOTP) Verify(otp string, count int) (bool, error) {
refOTP, err := h.At(count)
if err != nil {
return false, err
}
return otp == refOTP, nil
}
// 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.
func (h *HOTP) ProvisioningURI(accountName, issuerName string, initialCount int) (string, error) {
return buildURI(
otpTypeHOTP,
EncodeBase32(h.secret),
accountName,
issuerName,
h.hasher.HashName,
initialCount,
h.length,
0)
}