-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding.go
32 lines (29 loc) · 926 Bytes
/
coding.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
package cbor
import (
"github.com/dcnetio/gothreads-lib/crypto"
blocks "github.com/ipfs/go-block-format"
cbornode "github.com/ipfs/go-ipld-cbor"
format "github.com/ipfs/go-ipld-format"
mh "github.com/multiformats/go-multihash"
)
// EncodeBlock returns a node by encrypting the block's raw bytes with key.
func EncodeBlock(block blocks.Block, key crypto.EncryptionKey) (format.Node, error) {
coded, err := key.Encrypt(block.RawData())
if err != nil {
return nil, err
}
return cbornode.WrapObject(coded, mh.SHA2_256, -1)
}
// DecodeBlock returns a node by decrypting the block's raw bytes with key.
func DecodeBlock(block blocks.Block, key crypto.DecryptionKey) (format.Node, error) {
var raw []byte
err := cbornode.DecodeInto(block.RawData(), &raw)
if err != nil {
return nil, err
}
decoded, err := key.Decrypt(raw)
if err != nil {
return nil, err
}
return cbornode.Decode(decoded, mh.SHA2_256, -1)
}