-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_alice.go
46 lines (38 loc) · 1.01 KB
/
auth_alice.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
package lamport
import (
"crypto/sha256"
"errors"
"golang.org/x/exp/slices"
)
type OneTimeAuthPassword [32]byte
type OneTimeAuthAlice struct {
secret OneTimeAuthPassword
passwords []OneTimeAuthPassword
offset int
}
func NewOneTimeAuthAlice(n int) *OneTimeAuthAlice {
secret := randHex()
return &OneTimeAuthAlice{secret: secret, passwords: makePasswords(secret, n)}
}
func (alice *OneTimeAuthAlice) InitialPassword() OneTimeAuthPassword {
return alice.passwords[0]
}
func (alice *OneTimeAuthAlice) NextPassword() (*OneTimeAuthPassword, error) {
if alice.offset >= len(alice.passwords) {
return nil, errors.New("all passwords used")
}
alice.offset += 1
return &alice.passwords[alice.offset-1], nil
}
func makePasswords(secret OneTimeAuthPassword, n int) []OneTimeAuthPassword {
passwords := make([]OneTimeAuthPassword, n)
for i := 0; i < n; i++ {
if i == 0 {
passwords[i] = secret
continue
}
passwords[i] = sha256.Sum256(passwords[i-1][:])
}
slices.Reverse(passwords)
return passwords
}