forked from fangj99/spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
58 lines (50 loc) · 1.2 KB
/
token.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
package spider
import (
"crypto/sha1"
"encoding/hex"
"io"
"math/big"
"math/rand"
"time"
)
//ID define
type ID []byte
func (id ID) String() string {
return hex.EncodeToString(id)
}
//Int get int
func (id ID) Int() *big.Int {
return big.NewInt(0).SetBytes(id)
}
//Neighbor get neighbor
func (id ID) Neighbor(tableID ID) ID {
return append(id[:6], tableID[6:]...)
}
//Neightor get neighbor
func Neightor(id, tableID string) string {
return id[:6] + tableID[6:]
}
//GenerateID get id
func GenerateID() ID {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
hash := sha1.New()
io.WriteString(hash, time.Now().String())
io.WriteString(hash, string(random.Int()))
return hash.Sum(nil)
}
//GenerateIDList for uniform
func GenerateIDList(count int64) (ids []ID) {
if count <= 0 {
return
}
max := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
num := big.NewInt(0).SetBytes(max)
step := big.NewInt(0).Div(num, big.NewInt(count+2))
for i := 1; i < int(count+1); i++ {
item := big.NewInt(0).Mul(step, big.NewInt(int64(i)))
item.Add(item, big.NewInt(int64(rand.Intn(99))))
ids = append(ids, item.Bytes())
}
return
}