/
creator.go
104 lines (94 loc) · 2.29 KB
/
creator.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
package cdr
import (
"bytes"
"compress/gzip"
"context"
"crypto/rand"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/chacha20"
)
type PostFunc = func(ctx context.Context, data []byte) (*Ref, error)
type Middleware = func(ctx context.Context, in []byte, next PostFunc) (*Ref, error)
type Creator struct {
layers []Middleware
postFunc PostFunc
}
func NewCreator(layers []Middleware, postFunc PostFunc) *Creator {
return &Creator{
layers: layers,
postFunc: postFunc,
}
}
func (c *Creator) MakeRef(ctx context.Context, data []byte) (*Ref, error) {
var getPostFunc func(i int) PostFunc
getPostFunc = func(i int) PostFunc {
if i >= len(c.layers) {
return c.postFunc
}
return func(ctx context.Context, in []byte) (*Ref, error) {
return c.layers[i](ctx, in, getPostFunc(i+1))
}
}
return getPostFunc(0)(ctx, data)
}
func CompressGzip(ctx context.Context, in []byte, next PostFunc) (*Ref, error) {
buf := bytes.NewBuffer(nil)
gw := gzip.NewWriter(buf)
if _, err := gw.Write(in); err != nil {
return nil, err
}
if err := gw.Close(); err != nil {
return nil, err
}
ref, err := next(ctx, buf.Bytes())
if err != nil {
return nil, err
}
return &Ref{
Body: &Ref_Compress{Compress: &Compress{
Inner: ref,
Algo: CompressAlgo_GZIP,
}},
}, nil
}
func HashBlake2b256(ctx context.Context, in []byte, next PostFunc) (*Ref, error) {
sum := blake2b.Sum256(in)
ref, err := next(ctx, in)
if err != nil {
return nil, err
}
return &Ref{
Body: &Ref_ContentHash{ContentHash: &ContentHash{
Inner: ref,
Algo: HashAlgo_BLAKE2b_256,
Hash: sum[:],
}},
}, nil
}
// EncryptChaCha20 encrypts the data with a random key (stored in the ref)
// Does not protect the data from tampering.
func EncryptChaCha20(ctx context.Context, data []byte, next PostFunc) (*Ref, error) {
nonce := make([]byte, chacha20.NonceSize)
key := make([]byte, 32)
if _, err := rand.Read(key[:]); err != nil {
return nil, err
}
cipher, err := chacha20.NewUnauthenticatedCipher(key, nonce)
if err != nil {
panic(err)
}
ctext := make([]byte, len(data))
cipher.XORKeyStream(ctext, data)
ref, err := next(ctx, ctext)
if err != nil {
return nil, err
}
return &Ref{
Body: &Ref_Cipher{Cipher: &Cipher{
Inner: ref,
Algo: CipherAlgo_CHACHA20,
Key: key,
Nonce: nonce,
}},
}, nil
}