-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (44 loc) · 944 Bytes
/
util.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
package kademlia
import (
"io/ioutil"
"math/bits"
"net/http"
"github.com/google/uuid"
"github.com/minio/sha256-simd"
)
const (
_externalIPcheckerAddress = "https://myexternalip.com/raw"
)
func XOR(a, b []byte) []byte {
c := make([]byte, len(a))
for i := 0; i < len(a); i++ {
c[i] = a[i] ^ b[i]
}
return c
}
func CommonPrefixLen(a, b []byte) int {
hashedID := XOR(a, b)
for i, b := range hashedID {
if b != 0 {
return i*8 + bits.LeadingZeros8(uint8(b))
}
}
return len(hashedID) * 8
}
// ConvertPeerID creates a DHT ID by hashing a Peer ID (Multihash)
func ConvertPeerID(id string) []byte {
hash := sha256.Sum256([]byte(id))
return hash[:]
}
func GetMyIP() (string, error) {
resp, err := http.Get(_externalIPcheckerAddress)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
return string(content), nil
}
func NewUUIDv4() string {
return uuid.NewString()
}