-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
64 lines (55 loc) · 1.14 KB
/
otp.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
package otp
import (
"crypto/sha1"
"hash"
)
type OTP interface {
Generate(in interface{}, opts ...Option) string
Validate(in interface{}, code string, opts ...Option) bool
}
type Options struct {
Digit int
HashFunc func() hash.Hash
TimeStep int
Leeway int
StartTime int64
}
func NewOptions() *Options {
return &Options{
Digit: 6,
HashFunc: sha1.New,
TimeStep: 30,
StartTime: 0,
}
}
type Option func(*Options)
// WithDigit
// The default digit = 6
func WithDigit(digit int) Option {
return func(options *Options) {
options.Digit = digit
}
}
// WithHashFunc
// The default hash func is sha1.New
// You can use other hash func such as: sha256.New, sha512.New, md5.New
func WithHashFunc(f func() hash.Hash) Option {
return func(options *Options) {
options.HashFunc = f
}
}
// WithTimeStep
// The default time step is 30s.
func WithTimeStep(step int) Option {
return func(options *Options) {
options.TimeStep = step
}
}
// WithStartTime
// The default start time = 0
// The UNIX time to start counting time steps
func WithStartTime(start int64) Option {
return func(options *Options) {
options.StartTime = start
}
}