-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.go
More file actions
117 lines (95 loc) · 2.5 KB
/
encoding.go
File metadata and controls
117 lines (95 loc) · 2.5 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package shamir
import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"strings"
)
const (
tlvX byte = 0x01
tlvL byte = 0x02
tlvY byte = 0x03
textPrefix = "shamir1:"
)
// EncodeShare encodes a Share into a binary TLV format.
func EncodeShare(s Share) ([]byte, error) {
if s.X == 0 {
return nil, errors.New("invalid share X value")
}
if len(s.Y) == 0 {
return nil, errors.New("empty share payload")
}
var buf bytes.Buffer
// X
buf.WriteByte(tlvX)
buf.WriteByte(s.X)
// Length
buf.WriteByte(tlvL)
if err := binary.Write(&buf, binary.BigEndian, uint16(len(s.Y))); err != nil {
return nil, err
}
// Y
buf.WriteByte(tlvY)
buf.Write(s.Y)
return buf.Bytes(), nil
}
// DecodeShare decodes a Share from a binary TLV format.
func DecodeShare(data []byte) (Share, error) {
var s Share
r := bytes.NewReader(data)
for r.Len() > 0 {
tag, err := r.ReadByte()
if err != nil {
return s, err
}
switch tag {
case tlvX:
x, err := r.ReadByte()
if err != nil {
return s, err
}
s.X = x
case tlvL:
var l uint16
if err := binary.Read(r, binary.BigEndian, &l); err != nil {
return s, err
}
s.Y = make([]byte, l)
case tlvY:
if len(s.Y) == 0 {
return s, errors.New("missing length before payload")
}
if _, err := r.Read(s.Y); err != nil {
return s, err
}
default:
return s, errors.New("unknown TLV tag")
}
}
if s.X == 0 || len(s.Y) == 0 {
return s, errors.New("incomplete share encoding")
}
return s, nil
}
// MarshalText encodes a Share into a versioned Base64URL string.
func MarshalText(s Share) (string, error) {
bin, err := EncodeShare(s)
if err != nil {
return "", err
}
enc := base64.RawURLEncoding.EncodeToString(bin)
return textPrefix + enc, nil
}
// UnmarshalText decodes a Share from a versioned Base64URL string.
func UnmarshalText(text string) (Share, error) {
if !strings.HasPrefix(text, textPrefix) {
return Share{}, errors.New("invalid share prefix")
}
raw := strings.TrimPrefix(text, textPrefix)
bin, err := base64.RawURLEncoding.DecodeString(raw)
if err != nil {
return Share{}, err
}
return DecodeShare(bin)
}