This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.go
105 lines (100 loc) · 2.64 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
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
package dare
import (
"io"
"os"
"path/filepath"
config "github.com/da-moon/go-dare/internal/config"
decryptor "github.com/da-moon/go-dare/internal/decryptor"
model "github.com/da-moon/go-dare/model"
files "github.com/da-moon/go-files"
stream "github.com/da-moon/go-stream"
stacktrace "github.com/palantir/stacktrace"
)
// DecryptWithWriter ...
func DecryptWithWriter(
dstwriter io.Writer,
srcReader io.Reader,
key [32]byte,
nonce [24]byte,
) error {
decWriter := decryptor.NewWriter(dstwriter, nonce, &key)
for {
buffer := make([]byte, config.DefaultChunkSize+config.DefaultOverhead)
bytesRead, err := srcReader.Read(buffer)
if err == io.EOF {
break
} else if err != nil {
return err
}
_, err = decWriter.Write(buffer[:bytesRead])
if err != nil {
return err
}
}
return nil
}
// DecryptFile ...
func DecryptFile(r *model.DecryptRequest) (*model.DecryptResponse, error) {
var err error
if r == nil {
err = stacktrace.NewError("nil request")
return nil, err
}
err = r.Sanitize()
if err != nil {
err = stacktrace.Propagate(err, "could not sanitize request")
return nil, err
}
result := r.Response()
for k, v := range r.Targets {
srcFile, _, err := files.SafeOpenPath(k)
if err != nil {
err = stacktrace.Propagate(err, "could not decrypt '%s'", k)
return nil, err
}
defer srcFile.Close()
os.Remove(v)
files.MkdirAll(filepath.Dir(v))
destinationFile, err := os.Create(v)
if err != nil {
err = stacktrace.NewError("could not successfully create a new empty file for %s", v)
return nil, err
}
defer destinationFile.Close()
dstWriter, err := stream.NewHashWriter(
destinationFile,
stream.WithMD5(),
stream.WithSHA256(),
)
if err != nil {
err = stacktrace.Propagate(err, "Could create a new hashwriter for '%s' ", k)
return nil, err
}
defer stream.ShutdownMD5Hasher()
err = DecryptWithWriter(dstWriter, srcFile, r.Key, r.Nonce)
if err != nil {
err = stacktrace.Propagate(err, "Could not decrypt file at '%s' and store it in '%s' ", k, v)
return nil, err
}
md5Hex, err := dstWriter.HexString(stream.MD5)
if err != nil {
err = stacktrace.Propagate(err, "could not calculate hex encoded md5 hash of '%s' ", v)
return nil, err
}
sha256Hex, err := dstWriter.HexString(stream.SHA256)
if err != nil {
err = stacktrace.Propagate(err, "could not calculate hex encoded sha256 hash of '%s' ", v)
return nil, err
}
result.DecryptedArtifacts[v] = model.Hash{
Md5: md5Hex,
Sha256: sha256Hex,
}
}
err = result.Sanitize()
if err != nil {
err = stacktrace.Propagate(err, "could not Sanitize Encrypt Response")
return nil, err
}
return result, nil
}