/
box_public_summary.go
68 lines (57 loc) · 1.62 KB
/
box_public_summary.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
package teams
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-codec/codec"
)
type boxPublicSummaryTable map[keybase1.UID]keybase1.Seqno
type boxPublicSummary struct {
table boxPublicSummaryTable
encoded []byte
}
func newBoxPublicSummary(d map[keybase1.UserVersion]keybase1.PerUserKey) (*boxPublicSummary, error) {
table := make(boxPublicSummaryTable, len(d))
for uv, puk := range d {
q, found := table[uv.Uid]
if !found || q < puk.Seqno {
table[uv.Uid] = puk.Seqno
}
}
return newBoxPublicSummaryFromTable(table)
}
// encode only ever gets called with the boxPublicSummary is being constructed. This means
// we don't allow mutation. Thus, we just encode it once, since if ever canonical encoding
// stops working, it won't matter, we'll still get consistent results.
func newBoxPublicSummaryFromTable(table boxPublicSummaryTable) (*boxPublicSummary, error) {
ret := boxPublicSummary{
table: table,
}
err := ret.encode()
if err != nil {
return nil, err
}
return &ret, nil
}
func (b *boxPublicSummary) encode() error {
var mh codec.MsgpackHandle
mh.WriteExt = true
mh.Canonical = true
err := codec.NewEncoderBytes(&b.encoded, &mh).Encode(b.table)
return err
}
func (b boxPublicSummary) Hash() []byte {
ret := sha256.Sum256(b.encoded)
return ret[:]
}
func (b boxPublicSummary) HashHexEncoded() string {
tmp := b.Hash()
return hex.EncodeToString(tmp)
}
func (b boxPublicSummary) EncodeToString() string {
return base64.StdEncoding.EncodeToString(b.encoded)
}
func (b boxPublicSummary) IsEmpty() bool {
return len(b.table) == 0
}