-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptedfilemanagment.go
More file actions
46 lines (37 loc) · 947 Bytes
/
encryptedfilemanagment.go
File metadata and controls
46 lines (37 loc) · 947 Bytes
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
package main
import (
"encoding/json"
)
var key = []byte("jdyw3ts4dkflp8orftr7vd6372jqzpta")
// readEncryptedFile decrypts and parses an encrypted file and stores the result in the interface called mapping
func readEncryptedFile(path string, mapping interface{}, logCh chan string) error {
ciphertext, err := readFile(path, logCh)
if err != nil {
return err
}
plaintext, err := decrypt(ciphertext, key)
if err != nil {
return err
}
err = json.Unmarshal(plaintext, mapping)
if err != nil {
return err
}
return nil
}
// writeEncryptedFile encrypts and writes a data object called mapping to a file
func writeEncryptedFile(path string, mapping interface{}, logCh chan string) error {
jsonString, err := json.Marshal(mapping)
if err != nil {
return err
}
encrypted, err := encrypt(jsonString, key)
if err != nil {
return err
}
err = writeFile(path, logCh, encrypted)
if err != nil {
return err
}
return nil
}