-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewGenerator.go
More file actions
44 lines (35 loc) · 834 Bytes
/
newGenerator.go
File metadata and controls
44 lines (35 loc) · 834 Bytes
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
package token
import (
"math/rand"
"github.com/Mobilpadde/characters/v2/letters"
)
// NewGenerator creates a new token generator
// with the given [Option]s.
func NewGenerator(opts ...Option) (*Generator, error) {
g := &Generator{
amount: 6, // Defaults to `6` as most other TOTP codes are this length
}
for _, opt := range opts {
if err := opt(g); err != nil {
return nil, err
}
}
if g.secret == nil {
// set a default secret since none has been specified
secret := randSecret(32)
g.SetSecret(secret)
}
// now := time.Now().UTC()
// g.timing.curr = now
// g.timing.last = now.Add(-g.period)
return g, nil
}
func randSecret(n int) string {
chars := letters.Combined
charsLength := len(chars)
b := make([]byte, n)
for i := range b {
b[i] = chars[rand.Intn(charsLength)]
}
return string(b)
}