-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
62 lines (52 loc) · 1.34 KB
/
hash.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
59
60
61
62
package passtor
import (
"encoding/base64"
"encoding/hex"
"fmt"
"golang.org/x/crypto/sha3"
)
// HASHSIZE size of a hash in byte
const HASHSIZE = 64
// Hash is a flexible type to handle hashes
type Hash [HASHSIZE]byte
// H hashes the given bytes value
func H(data []byte) Hash {
return sha3.Sum512(data)
}
// XOR function computing the XOR distance between two hashes
func (hash0 Hash) XOR(hash1 Hash) Hash {
res := Hash{}
for i := range hash0[:] {
res[i] = hash0[i] ^ hash1[i]
}
return res
}
// Compare two hashes, returns 1 if first hash smaller than the second, -1 if
// the second is smaller than the first, and 0 if they are equal
func (hash0 Hash) Compare(hash1 Hash) int {
for i := 0; i < len(hash0); i++ {
if hash0[i] < hash1[i] {
return -1
} else if hash0[i] > hash1[i] {
return 1
}
}
return 0
}
// String base64 representation of Hash
func (hash0 Hash) String() string {
return base64.StdEncoding.EncodeToString(hash0[:])
}
// Hex representation of Hash
func (hash0 Hash) Hex() string {
return hex.EncodeToString(hash0[:])
}
// PrintDistancesToHash print the distance from a list of node addresses to
// a hash
func (hash0 Hash) PrintDistancesToHash(list []NodeAddr) {
str := "Printing distance to " + hash0.String() + ":\n"
for _, a := range list {
str += fmt.Sprintln(a.NodeID.XOR(hash0).Hex(), a.Addr)
}
fmt.Print(str)
}