-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotp.go
76 lines (62 loc) · 1.73 KB
/
totp.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gotp
import "time"
// time-based OTP counters.
type TOTP struct {
OTP
interval int
}
func NewTOTP(secret string, digits, interval int, hasher *Hasher) *TOTP {
otp := NewOTP(secret, digits, hasher)
return &TOTP{OTP: otp, interval: interval}
}
func NewDefaultTOTP(secret string) *TOTP {
return NewTOTP(secret, 6, 30, nil)
}
// Generate time OTP of given timestamp
func (t *TOTP) At(timestamp int) string {
return t.generateOTP(t.timecode(timestamp))
}
// Generate the current time OTP
func (t *TOTP) Now() string {
return t.At(currentTimestamp())
}
// Generate the current time OTP and expiration time
func (t *TOTP) NowWithExpiration() (string, int64) {
interval64 := int64(t.interval)
timeCodeInt64 := time.Now().Unix() / interval64
expirationTime := (timeCodeInt64 + 1) * interval64
return t.generateOTP(int(timeCodeInt64)), expirationTime
}
/*
Verify OTP.
params:
otp: the OTP to check against
timestamp: time to check OTP at
*/
func (t *TOTP) Verify(otp string, timestamp int) bool {
return otp == t.At(timestamp)
}
/*
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.
See also:
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
params:
accountName: name of the account
issuerName: the name of the OTP issuer; this will be the organization title of the OTP entry in Authenticator
returns: provisioning URI
*/
func (t *TOTP) ProvisioningUri(accountName, issuerName string) string {
return BuildUri(
OtpTypeTotp,
t.secret,
accountName,
issuerName,
t.hasher.HashName,
0,
t.digits,
t.interval)
}
func (t *TOTP) timecode(timestamp int) int {
return int(timestamp / t.interval)
}