-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.go
74 lines (59 loc) · 1.27 KB
/
decrypt.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
package main
import (
"log"
"os"
"strings"
"github.com/s-gheldd/aftercrypt/acrypt"
)
func decryptFile(secret []byte, relPath string, done chan<- struct{}, errs chan<- error) {
absPath, err := sanityCheckFile(relPath)
if err != nil {
logErrAndSignalDone(err, done)
return
}
input, err := os.Open(absPath)
if err != nil {
logErrAndSignalDone(err, done)
return
}
defer input.Close()
aFile, err := acrypt.Deserialize(input)
if err != nil {
logErrAndSignalDone(err, done)
return
}
cipher, err := acrypt.GCMCipher(secret, aFile.Key.Salt)
if err != nil {
logErrAndSignalDone(err, done)
return
}
payload := aFile.Payload
payload, err = cipher.Open(payload[:0], aFile.Nonce, payload, nil)
if err != nil {
logErrAndSignalDone(err, done)
return
}
output, err := os.Create(outPutFileName(absPath))
if err != nil {
logErrAndSignalDone(err, done)
return
}
defer output.Close()
_, err = output.Write(payload)
if err != nil {
logErrAndSignalDone(err, done)
return
}
done <- struct{}{}
}
func outPutFileName(absPath string) string {
if strings.HasSuffix(absPath, acrypt.Extension) {
return absPath[:len(absPath)-len(acrypt.Extension)]
}
return absPath + ".dec"
}
func errorHandler(errs <-chan error) {
for {
log.Println(<-errs)
}
}