-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpapersave-source.go
267 lines (221 loc) · 5.92 KB
/
papersave-source.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright © 2020 Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What The Fuck You Want
// To Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/wtfpl/COPYING for more details.
package main
import (
"bytes"
"fmt"
"io"
"os"
"compress/gzip"
"encoding/base64"
"path/filepath"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
"github.com/hashicorp/vault/shamir"
)
// PSFile
type PSFile struct {
// the file full path.
filename string
//
// Folloring fields are only used to pass to Share.
// TODO: Simplify this process.
//
// The file basenane
basename string
dirname string
// The file size
filesize int64
// The file last change in os.Stats ModTime string format.
lastChange string
// File* and Binary* checksums
file *Checksums
binary *Checksums
binaryb64 *Checksums
password string
Shares []*Share
// CLI options
opts *Create
}
func NewPSFile(opts *Create) (p *PSFile, err error) {
info, err := os.Stat(opts.File)
// fmt.Printf("%#v\n", info.Size())
p = &PSFile{
filename: opts.File,
dirname: filepath.Dir(opts.File),
basename: filepath.Base(opts.File),
lastChange: fmt.Sprintf("%s", info.ModTime()),
filesize: info.Size(),
opts: opts,
}
if opts.Encrypt {
if p.opts.Password != "" {
p.password = opts.Password
} else {
p.password = generatePassword(16)
}
}
binary, err := p.readFile()
if err != nil {
return
}
err = p.createShares(binary)
return
}
func (p *PSFile) createShares(data []byte) (err error) {
var shares [][]byte
if p.opts.Shares == 1 {
shares = make([][]byte, 1)
shares[0] = data
} else {
shares, err = shamir.Split(data, p.opts.Shares, p.opts.Thresholds)
if err != nil {
return
}
}
p.Shares = make([]*Share, p.opts.Shares)
for i, share := range shares {
var b64Buffer bytes.Buffer
// b64w writes into b64Buffer
b64w := base64.NewEncoder(base64.StdEncoding, &b64Buffer)
binarychk := newChksum()
mw1 := io.MultiWriter(append(binarychk.Writers(), b64w)...)
mw1.Write(share)
b64w.Close()
s := &Share{
ID: i + 1,
Binary: binarychk.Checksums(),
SourceFile: p.file,
SourceBinary: p.binary,
SourceBinaryB64: p.binaryb64,
Basename: p.basename,
dirname: fmt.Sprintf("%s/%%s.papersave", p.dirname),
Filesize: p.filesize,
LastChange: p.lastChange,
Password: p.password,
ShowPassword: p.opts.ShowPassword,
}
s.Blocks, err = newBlocks(s.ID, b64Buffer.Bytes())
if err != nil {
return
}
// Compute the base64 encoded data checksums.
b64chk := newChksum()
mw2 := io.MultiWriter(b64chk.Writers()...)
mw2.Write(s.Base64Blocks(true))
s.B64 = b64chk.Checksums()
p.Shares[i] = s
}
return
}
func CombineShares(shares []*Share) (ret []byte, err error) {
bufs := make([][]byte, len(shares))
for i, s := range shares {
bufs[i], err = base64.StdEncoding.DecodeString(string(s.Base64Blocks(true)))
if err != nil {
return
}
}
secret, err := shamir.Combine(bufs)
if err != nil {
return
}
var b64Buffer bytes.Buffer
b64w := base64.NewEncoder(base64.StdEncoding, &b64Buffer)
b64w.Write(secret)
b64w.Close()
ret = b64Buffer.Bytes()
return
}
// readFile process p.filename and return encoded byte array.
//
// First the file is read and checksums (md5, sha1, sha256) are computed and
// stored in their respective p.File* fields.
//
// Then file content is compressed using gzip with best compression ratio.
//
// Next if the content should be encrypted, a GPG symetric encryption is
// made against the compressed data.
//
// The binary encoded file (gzip or gzip+gpg) checksums are computed and
// stored in their respective p.Binary* fields and its content is returned
// in a byte array.
func (p *PSFile) readFile() (ret []byte, err error) {
var dataBuffer bytes.Buffer
// Open source file
f, err := os.Open(p.filename)
if err != nil {
return
}
defer f.Close()
binarychk := newChksum()
// mw2 handles the binary encoded result (gzip or gzip+gpg)
mw2 := io.MultiWriter(append(binarychk.Writers(), &dataBuffer)...)
var (
zw *gzip.Writer
gpg io.WriteCloser
)
// TODO: find a passtrough io.WriteClose simplify this part.
if !p.opts.Encrypt {
// If no encryption zw writes directly into mw2.
zw, err = gzip.NewWriterLevel(mw2, gzip.BestCompression)
// err is checked after if block.
} else {
// if encryption is required, zw writes into gpg which writes into
// mw2.
var (
pConfig packet.Config
fileHints openpgp.FileHints
)
fileHints.IsBinary = true
gpg, err = openpgp.SymmetricallyEncrypt(mw2, []byte(p.password),
&fileHints, &pConfig)
if err != nil {
return
}
zw, err = gzip.NewWriterLevel(gpg, gzip.BestCompression)
// err is checked after if block.
}
// Check if zw generated an error
if err != nil {
return
}
filechk := newChksum()
// mw1 computes checksums of original file and writes into zw.
mw1 := io.MultiWriter(append(filechk.Writers(), zw)...)
_, err = io.Copy(mw1, f)
if err != nil {
return
}
// Explicitly close both zw and gpg to flush all data to prevent errors
// such as:
// gpg: block_filter: 1st length byte missing
//
// or
// gunzip: (stdin): unexpected end of file
zw.Close()
if p.opts.Encrypt {
gpg.Close()
}
// Store checksums to PSFile.
p.file = filechk.Checksums()
p.binary = binarychk.Checksums()
ret = dataBuffer.Bytes()
// Compute binary base64
binaryb64chk := newChksum()
var b64Buffer bytes.Buffer
b64w := base64.NewEncoder(base64.StdEncoding, &b64Buffer)
b64w.Write(ret)
b64w.Close()
mw := io.MultiWriter(binaryb64chk.Writers()...)
for _, l := range byteWrap(b64Buffer.Bytes(), lineLength) {
mw.Write(l)
mw.Write([]byte("\n"))
}
p.binaryb64 = binaryb64chk.Checksums()
return
}