/
asset.go
250 lines (216 loc) · 6.35 KB
/
asset.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Copyright (c) 2018 Zettant Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bbclib
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
"github.com/ugorji/go/codec"
)
/*
BBcAsset definition
"IDLength" and "digestCalculating" are not included in a packed data. They are for internal use only.
"AssetID" is the SHA256 digest of packed BBcAsset data, which contains from "UserID" to "AssetBody".
The length of "AssetID" and "UserID" is defined by "IDLength".
"Nonce" is automatically determined with random value.
BBcAsset can contain a digest of a file, string, map[string]interface{} object as asset.
*/
type (
BBcAsset struct {
IdLengthConf *BBcIdConfig
Version uint32
digestCalculating bool
AssetID []byte
UserID []byte
Nonce []byte
AssetFileSize uint32
AssetFileDigest []byte
AssetBodyType uint16
AssetBodySize uint16
AssetBody []byte
}
)
// An object for messagepack encoding/decoding
var (
mh codec.MsgpackHandle
)
// encodeMessagePack encodes object in messagepack data
func encodeMessagePack(values interface{}) ([]byte, error) {
buf := make([]byte, 0, 65536)
err := codec.NewEncoderBytes(&buf, &mh).Encode(values)
if err != nil {
return nil, err
}
return buf, nil
}
// decodeMessagePack decodes messagepack data
func decodeMessagePack(buf []byte) (interface{}, error) {
var values interface{}
err := codec.NewDecoderBytes(buf, &mh).Decode(&values)
if err != nil {
return nil, err
}
return values, nil
}
// Stringer outputs the content of the object
func (p *BBcAsset) Stringer() string {
ret := " Asset:\n"
ret += fmt.Sprintf(" asset_id: %x\n", p.AssetID)
ret += fmt.Sprintf(" user_id: %x\n", p.UserID)
ret += fmt.Sprintf(" nonce: %x\n", p.Nonce)
ret += fmt.Sprintf(" file_size: %d\n", p.AssetFileSize)
if p.AssetFileDigest != nil {
ret += fmt.Sprintf(" file_digest: %x\n", p.AssetFileDigest)
} else {
ret += " file_digest: None\n"
}
ret += fmt.Sprintf(" body_size: %d\n", p.AssetBodySize)
ret += fmt.Sprintf(" body: %v\n", p.AssetBody)
return ret
}
// Set ID length configuration
func (p *BBcAsset) SetIdLengthConf(conf * BBcIdConfig) {
p.IdLengthConf = conf
}
// Add sets userID in the BBcAsset object
func (p *BBcAsset) Add(userID *[]byte) {
if userID != nil {
p.UserID = make([]byte, p.IdLengthConf.UserIdLength)
copy(p.UserID, (*userID)[:p.IdLengthConf.UserIdLength])
}
p.Nonce = GetRandomValue(p.IdLengthConf.NonceLength)
}
// AddFile add the digest of file in the BBcAsset object
// Note that this method adds the SHA256 digest of the file content (not file binary itself)
func (p *BBcAsset) AddFile(fileContent *[]byte) {
p.AssetFileSize = uint32(binary.Size(fileContent))
digest := sha256.Sum256(*fileContent)
p.AssetFileDigest = digest[:]
}
// AddBody sets data in the BBcAsset object
func (p *BBcAsset) AddBody(bodyContent interface{}) {
if body, ok := bodyContent.(string); ok {
p.AssetBody = []byte(body)
p.AssetBodySize = uint16(len(body))
} else if body, ok := bodyContent.([]byte); ok {
copy(p.AssetBody, body)
p.AssetBodySize = uint16(len(body))
} else {
_ = p.AddBodyObject(bodyContent)
}
}
// AddBodyString sets a string data in the BBcAsset object
func (p *BBcAsset) AddBodyString(bodyContent string) {
p.AssetBodyType = 0
p.AssetBody = []byte(bodyContent)
p.AssetBodySize = uint16(len(bodyContent))
}
// AddBodyObject sets an object data in the BBcAsset object and convert it in MessagePack format
func (p *BBcAsset) AddBodyObject(bodyContent interface{}) error {
p.AssetBodyType = 1
var err error
p.AssetBody, err = encodeMessagePack(bodyContent)
if err != nil {
return err
}
p.AssetBodySize = uint16(len(p.AssetBody))
return nil
}
// GetBodyObject returns the object which is in MessagePack format
func (p *BBcAsset) GetBodyObject() (interface{}, error) {
if p.AssetBodyType != 1 {
return nil, nil
}
return decodeMessagePack(p.AssetBody)
}
// Digest calculates the SHA256 digest of the AssetID value of the BBcAsset object
func (p *BBcAsset) Digest() []byte {
p.digestCalculating = true
asset, err := p.Pack()
if err != nil {
p.digestCalculating = false
return nil
}
digest := sha256.Sum256(asset)
if p.AssetID == nil {
p.AssetID = make([]byte, p.IdLengthConf.AssetIdLength)
}
p.AssetID = digest[:p.IdLengthConf.AssetIdLength]
p.digestCalculating = false
return digest[:]
}
// Pack returns the binary data of the BBcAsset object
func (p *BBcAsset) Pack() ([]byte, error) {
buf := new(bytes.Buffer)
if !p.digestCalculating {
if p.AssetID == nil {
p.Digest()
}
PutBigInt(buf, &p.AssetID, p.IdLengthConf.AssetIdLength)
}
PutBigInt(buf, &p.UserID, p.IdLengthConf.UserIdLength)
PutBigInt(buf, &p.Nonce, len(p.Nonce))
Put4byte(buf, p.AssetFileSize)
if p.AssetFileSize > 0 {
PutBigInt(buf, &p.AssetFileDigest, 32)
}
Put2byte(buf, p.AssetBodyType)
Put2byte(buf, p.AssetBodySize)
if p.AssetBodySize > 0 {
if err := binary.Write(buf, binary.LittleEndian, p.AssetBody); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
// Unpack the BBcAsset object to the binary data
func (p *BBcAsset) Unpack(dat *[]byte) error {
if p.IdLengthConf == nil {
p.IdLengthConf = &BBcIdConfig{}
}
var err error
buf := bytes.NewBuffer(*dat)
p.AssetID, p.IdLengthConf.AssetIdLength, err = GetBigInt(buf)
if err != nil {
return err
}
p.UserID, p.IdLengthConf.UserIdLength, err = GetBigInt(buf)
if err != nil {
return err
}
p.Nonce, p.IdLengthConf.NonceLength, err = GetBigInt(buf)
if err != nil {
return err
}
p.AssetFileSize, err = Get4byte(buf)
if err != nil {
return err
}
if p.AssetFileSize > 0 {
p.AssetFileDigest, _, err = GetBigInt(buf)
if err != nil {
return err
}
}
p.AssetBodyType, err = Get2byte(buf)
if err != nil {
return err
}
p.AssetBodySize, err = Get2byte(buf)
if err != nil {
return err
}
p.AssetBody, _, err = GetBytes(buf, int(p.AssetBodySize))
return err
}