/
encrypt.go
96 lines (79 loc) · 2.03 KB
/
encrypt.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
package v1
import (
"crypto/cipher"
"errors"
"hash"
"io"
"os"
)
var (
ErrBadPass = errors.New("password cannot be zero bytes")
)
//NewEnc takes a password, key derivation cost parameters, and an io.Writer and returns
//an io.WriteCloser that encrypts the data written to it.
//
//Close must be called on the returned io.WriteCloser when finished writing
//and before the underlying io.Writer is closed, otherwise the WriteCloser will
//not know when to write the hmac-sha512 signature of the encrypted data
func NewEnc(pass []byte, cp CostParams, w io.Writer) (io.WriteCloser, error) {
if len(pass) == 0 {
return nil, ErrBadPass
}
ch := newCryptoHeader(&costParams{
time: cp.Time,
memory: cp.Memory,
threads: cp.Threads,
})
cr := newCryptoRing(pass, ch)
headerData, _ := ch.MarshalBinary()
ew := &encWriter{w: w, mac: cr.Mac(), cr: cr}
sw := &cipher.StreamWriter{S: cr.Stream(), W: ew}
if _, err := ew.Write(headerData); err != nil {
return nil, err
}
return sw, nil
}
type encWriter struct {
w io.Writer
mac hash.Hash
cr *cryptoRing
}
// Write encrypts b and writes it to the underlying writer
func (e *encWriter) Write(b []byte) (int, error) {
n, err := e.w.Write(b)
_, macErr := e.mac.Write(b[:n])
if err != nil {
return n, err
}
return n, macErr
}
// Close must be called once finished writing to e and before closing the underlying writer
func (e *encWriter) Close() error {
sig := e.mac.Sum(nil)
e.w.Write(sig)
e.cr.Destroy()
if c, ok := e.w.(io.Closer); ok {
return c.Close()
}
return nil
}
// EncryptFile will encrypt fileIn and store the encrypted result at fileOut
func EncryptFile(pass []byte, cp CostParams, fileIn, fileOut string) error {
encF, err := os.OpenFile(fileOut, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return err
}
defer encF.Close()
encW, err := NewEnc(pass, cp, encF)
if err != nil {
return err
}
defer encW.Close()
plainFile, err := os.Open(fileIn)
if err != nil {
return err
}
defer plainFile.Close()
_, err = io.Copy(encW, plainFile)
return err
}