-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
45 lines (37 loc) · 1.07 KB
/
utils.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
package kbucket
import (
"crypto/rand"
"crypto/sha1"
"net/netip"
)
// CompareAddrPorts compares two netip.AddrPorts.
// It will return true if the two AddrPorts are equal, false otherwise.
func CompareAddrPorts(a, b netip.AddrPort) bool {
if a.Addr().Unmap().Compare(b.Addr().Unmap()) == 0 && a.Port() == b.Port() {
return true
}
return false
}
// GenerateId generates a random 160-bit ID (SHA-1).
// It will return an error if the system's secure random number generator fails
// to function correctly, in which case the caller should not continue.
func GenerateId() ([]byte, error) {
b, err := GenerateRandomBytes(20)
if err != nil {
return nil, err
}
h := sha1.New()
h.Write(b)
return h.Sum(nil), nil
}
// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random number generator fails
// to function correctly, in which case the caller should not continue.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}