-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffer.go
177 lines (119 loc) · 3.59 KB
/
coffer.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
package coffer
import (
"encoding/base64"
"log"
"os"
"path"
"github.com/wolfeidau/coffer/nacl"
)
var (
// CofferBlockSize size of the key
CofferBlockSize = 32
// OwnerRead is the default mode set for new coffer files, note the octal number
OwnerRead = os.FileMode(0600)
// Version the version of the coffer file
Version = "2.0.0"
)
// MustEncrypt encrypt the supplied file
func MustEncrypt(cofferFile, alias string) {
data := mustReadFile(cofferFile)
payload := mustEncryptPayload(data, alias)
mustWriteFile(cofferFile, payload, OwnerRead)
}
// MustDecrypt decrypt the supplied file
func MustDecrypt(cofferFile, alias string) []byte {
data := mustReadFile(cofferFile)
payload := mustDecryptPayload(data, alias)
return mustWriteFile(cofferFile, payload, OwnerRead)
}
// MustSync sync the file up to S3
func MustSync(cofferFile, alias, base string) {
// base not set
if base == "" {
base = "/" // set the base directory to '/'
}
payload := mustReadFile(cofferFile)
// if the coffer file is encrypted, decrypt it
if isEncrypted(payload) {
payload = mustDecryptPayload(payload, alias)
}
bundle := mustDecodeBundle(payload)
bundle.MustValidate()
mustExtractBundle(bundle, base)
}
// MustUpload upload the file to the supplied s3 bucket
func MustUpload(cofferFile, alias, bucket string) {
payload := mustReadFile(cofferFile)
// if the coffer is not encrypted
if !isEncrypted(payload) {
payload = mustEncryptPayload(payload, alias)
}
filename := path.Base(cofferFile)
mustUpload(bucket, filename, payload)
}
// MustDownload download the file from the supplied s3 bucket
func MustDownload(cofferFile, alias, bucket string) {
filename := path.Base(cofferFile)
payload := mustDownload(bucket, filename)
mustWriteFile(cofferFile, payload, OwnerRead)
}
// MustDownloadSync download the file from the supplied s3 bucket and sync
// it to the filesystem
func MustDownloadSync(cofferFile, alias, bucket, base string) {
// base not set
if base == "" {
base = "/" // set the base directory to '/'
}
filename := path.Base(cofferFile)
payload := mustDownload(bucket, filename)
// if the coffer file is encrypted, decrypt it
if isEncrypted(payload) {
payload = mustDecryptPayload(payload, alias)
}
bundle := mustDecodeBundle(payload)
bundle.MustValidate()
mustExtractBundle(bundle, base)
}
func isEncrypted(data []byte) bool {
coffer, err := DecodeCoffer(data)
if err != nil {
return false
}
return coffer.Validate()
}
func mustEncryptPayload(data []byte, alias string) []byte {
if isEncrypted(data) {
log.Fatalf("coffer file already encrypted")
}
key := mustGenerateDataKey(alias)
payload := nacl.Encrypt(data, key.Plaintext)
cipherText := base64.StdEncoding.EncodeToString(payload)
keyCipherText := base64.StdEncoding.EncodeToString(key.CiphertextBlob)
log.Printf("encoded data len: %d", len(cipherText))
return mustEncodeCoffer(&Coffer{
Key: keyCipherText,
Version: Version,
CipherText: cipherText,
})
}
func mustDecryptPayload(data []byte, alias string) []byte {
coffer, err := DecodeCoffer(data)
if err != nil {
log.Fatalf("unable to decode coffer")
}
if coffer.Validate() {
keyData, err := base64.StdEncoding.DecodeString(coffer.Key)
if err != nil {
log.Fatalf("unable to decode key")
}
key := mustDecrypt(keyData)
decoded, err := base64.StdEncoding.DecodeString(coffer.CipherText)
if err != nil {
log.Fatalf("coffer file could not be decoded")
}
log.Printf("decoded data len: %d", len(decoded))
return nacl.Decrypt(decoded, key.Plaintext)
}
log.Fatalf("invalid coffer file, unable to decrypt")
return []byte{}
}