-
Notifications
You must be signed in to change notification settings - Fork 12
/
fixedsize.go
204 lines (175 loc) · 5.91 KB
/
fixedsize.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package zkidentity
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/companyzero/sntrup4591761"
)
// FixedSizeSignature is a 64-byte, fixed size signature. This is used as an
// alternative for 64-byte signatures to ensure compact encoding into json.
type FixedSizeSignature [64]byte
// FixedSizeEd25519PrivateKey is a 64-byte, fixed size private key.
type FixedSizeEd25519PrivateKey = FixedSizeSignature
// FixedSizeEd25519PublicKey is a 32-byte, fixed size ed25519 public key.
type FixedSizeEd25519PublicKey = ShortID
// FixedSizeDigest is a 32-byte, fixed size sha256 digest.
type FixedSizeDigest = ShortID
// String returns the hex encoding of the FixedSizeSignature.
func (u FixedSizeSignature) String() string {
return hex.EncodeToString(u[:])
}
// MarshalJSON marshals the id into a json string.
func (u FixedSizeSignature) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
// UnmarshalJSON unmarshals the json representation of an FixedSizeSignature.
func (u *FixedSizeSignature) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return u.FromString(s)
}
// FromString decodes s into an FixedSizeSignature. s must contain an
// hex-encoded signature of the correct length.
func (u *FixedSizeSignature) FromString(s string) error {
h, err := hex.DecodeString(s)
if err != nil {
return err
}
if len(h) != len(u) {
return fmt.Errorf("invalid FixedSizeSignature length: %d", len(h))
}
copy(u[:], h)
return nil
}
// FromBytes copies the signature from the given byte slice. The passed slice
// must have the correct length.
func (u *FixedSizeSignature) FromBytes(b []byte) error {
if len(b) != len(u) {
return fmt.Errorf("invalid FixedSizeSignature length: %d", len(b))
}
copy(u[:], b)
return nil
}
// FixedSizeSntrupPublicKey is a fixed size sntrup public key.
type FixedSizeSntrupPublicKey [sntrup4591761.PublicKeySize]byte
// String returns the hex encoding of the FixedSizeSntrupPublicKey.
func (u FixedSizeSntrupPublicKey) String() string {
return hex.EncodeToString(u[:])
}
// MarshalJSON marshals the id into a json string.
func (u FixedSizeSntrupPublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
// UnmarshalJSON unmarshals the json representation of an FixedSizeSntrupPublicKey.
func (u *FixedSizeSntrupPublicKey) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return u.FromString(s)
}
// FromString decodes s into an FixedSizeSntrupPublicKey. s must contain an hex-encoded FixedSizeSntrupPrivateKey of the
// correct length.
func (u *FixedSizeSntrupPublicKey) FromString(s string) error {
h, err := hex.DecodeString(s)
if err != nil {
return err
}
if len(h) != len(u) {
return fmt.Errorf("invalid FixedSizeSntrupPublicKey length: %d", len(h))
}
copy(u[:], h)
return nil
}
// FromBytes copies the key from the given byte slice. The passed slice
// must have the correct length.
func (u *FixedSizeSntrupPublicKey) FromBytes(b []byte) error {
if len(b) != len(u) {
return fmt.Errorf("invalid FixedSizeSntrupPublicKey length: %d", len(b))
}
copy(u[:], b)
return nil
}
// FixedSizeSntrupPrivateKey is a fixed size sntrup private key.
type FixedSizeSntrupPrivateKey [sntrup4591761.PrivateKeySize]byte
// String returns the hex encoding of the FixedSizeSntrupPrivateKey.
func (u FixedSizeSntrupPrivateKey) String() string {
return hex.EncodeToString(u[:])
}
// MarshalJSON marshals the id into a json string.
func (u FixedSizeSntrupPrivateKey) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
// UnmarshalJSON unmarshals the json representation of an FixedSizeSntrupPrivateKey.
func (u *FixedSizeSntrupPrivateKey) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return u.FromString(s)
}
// FromString decodes s into an FixedSizeSntrupPrivateKey. s must contain an hex-encoded ID of the
// correct length.
func (u *FixedSizeSntrupPrivateKey) FromString(s string) error {
h, err := hex.DecodeString(s)
if err != nil {
return err
}
if len(h) != len(u) {
return fmt.Errorf("invalid FixedSizeSntrupPrivateKey length: %d", len(h))
}
copy(u[:], h)
return nil
}
// FromBytes copies the key from the given byte slice. The passed slice
// must have the correct length.
func (u *FixedSizeSntrupPrivateKey) FromBytes(b []byte) error {
if len(b) != len(u) {
return fmt.Errorf("invalid FixedSizeSntrupPrivateKey length: %d", len(b))
}
copy(u[:], b)
return nil
}
// FixedSizeSntrupCiphertext is a fixed size byte slice capable of holding a
// sntrup4591761 cipher text that encodes as an hex string in json.
type FixedSizeSntrupCiphertext [sntrup4591761.CiphertextSize]byte
// String returns the hex encoding of the FixedSizeSntrupCiphertext.
func (u FixedSizeSntrupCiphertext) String() string {
return hex.EncodeToString(u[:])
}
// MarshalJSON marshals the id into a json string.
func (u FixedSizeSntrupCiphertext) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
// UnmarshalJSON unmarshals the json representation of a FixedSizeSntrupCiphertext.
func (u *FixedSizeSntrupCiphertext) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return u.FromString(s)
}
// FromString decodes s into an FixedSizeSntrupCiphertext. s must contain an hex-encoded ID of the
// correct length.
func (u *FixedSizeSntrupCiphertext) FromString(s string) error {
h, err := hex.DecodeString(s)
if err != nil {
return err
}
if len(h) != len(u) {
return fmt.Errorf("invalid FixedSizeSntrupCiphertext length: %d", len(h))
}
copy(u[:], h)
return nil
}
// FromBytes copies the short id from the given byte slice. The passed slice
// must have the correct length.
func (u *FixedSizeSntrupCiphertext) FromBytes(b []byte) error {
if len(b) != len(u) {
return fmt.Errorf("invalid FixedSizeSntrupCiphertext length: %d", len(b))
}
copy(u[:], b)
return nil
}