/
secret.go
57 lines (50 loc) · 849 Bytes
/
secret.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
package dht
import (
"crypto/sha1"
"math/rand"
"time"
)
type secret struct {
cur []byte
old []byte
rand *rand.Rand
}
func newSecret() (s *secret) {
s = &secret{
cur: make([]byte, 8),
old: make([]byte, 8),
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
s.rand.Read(s.cur)
copy(s.old, s.cur)
return
}
func (s *secret) Update() {
copy(s.old, s.cur)
s.rand.Read(s.cur)
}
func (s *secret) Create(b []byte) []byte {
return s.create(b, false)
}
func (s *secret) create(b []byte, old bool) []byte {
sec := s.cur
if old {
sec = s.old
}
h := sha1.New()
h.Write(sec)
h.Write(b)
return h.Sum(nil)
}
func (s *secret) Match(b, token []byte) bool {
t := string(token)
t1 := s.create(b, false)
if string(t1) == t {
return true
}
t2 := s.create(b, true)
if string(t2) == t {
return true
}
return false
}