-
Notifications
You must be signed in to change notification settings - Fork 13
/
hashes.go
98 lines (85 loc) · 1.76 KB
/
hashes.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package brutalinks
import (
"strconv"
"strings"
vocab "github.com/go-ap/activitypub"
"github.com/google/uuid"
)
// Hash is a local type for string, it should hold a [32]byte array actually
type Hash uuid.UUID
// AnonymousHash is the sha hash for the anonymous account
var (
AnonymousHash = Hash{}
SystemHash = Hash{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
)
func HashFromIRI(i vocab.IRI) Hash {
//_, h := path.Split()
pieces := strings.Split(strings.TrimRight(i.String(), "/"), "/")
for i := len(pieces) - 1; i >= 0; i-- {
piece := pieces[i]
if h := HashFromString(piece); h != AnonymousHash {
return h
}
}
return AnonymousHash
}
func HashFromItem(obj vocab.Item) Hash {
if obj == nil {
return AnonymousHash
}
iri := obj.GetLink()
if len(iri) == 0 {
return AnonymousHash
}
return HashFromIRI(iri)
}
func HashFromString(s string) Hash {
if len(s) == 0 {
return AnonymousHash
}
if _, err := strconv.ParseInt(s, 10, 64); err == nil {
hh := [16]byte{}
bs := []byte(s)
st := len(bs) - 1
eh := len(hh) - 1
for i := st; i >= 0; i-- {
hh[eh] = bs[i]
eh--
if eh == 0 {
break
}
}
return hh
}
if u, err := uuid.Parse(s); err == nil {
return Hash(u)
}
return AnonymousHash
}
// String returns the hash as a string
func (h Hash) String() string {
return uuid.UUID(h).String()
}
// MarshalText
func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}
func (h Hash) IsValid() bool {
return uuid.UUID(h).Time() > 0
}
type Hashes []Hash
func (h Hashes) Contains(s Hash) bool {
for _, hh := range h {
if hh == s {
return true
}
}
return false
}
func (h Hashes) String() string {
str := make([]string, len(h))
for i, hh := range h {
str[i] = hh.String()
}
return strings.Join(str, ", ")
}