-
Notifications
You must be signed in to change notification settings - Fork 2
/
decrypt.go
80 lines (66 loc) · 1.96 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
package main
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"filippo.io/age"
"filippo.io/age/armor"
)
// DecryptFileWithPassword takes the given file and decrypts it's content
func DecryptFileWithPassword(inputPath string, outputPath string, password string) (output string, err error) {
// Create Identity
identity, err := age.NewScryptIdentity(password)
if err != nil {
return "", errors.New("invalidPasswordError")
}
// Decrypt and return
return DecryptFile(inputPath, outputPath, []age.Identity{identity})
}
// DecryptFile takes the given file and decrypts it's content
func DecryptFile(inputPath string, outputPath string, identities []age.Identity) (output string, err error) {
// Read the input file
f, err := os.Open(inputPath)
if err != nil {
return "", errors.New("inputPathError")
}
// Prepare the Decryption Reader. Use Armor if needed
var ageReader io.Reader
if b, _ := ioutil.ReadFile(inputPath); strings.HasPrefix(string(b), "-----BEGIN AGE ENCRYPTED FILE-----") {
armorReader := armor.NewReader(f)
ageReader, err = age.Decrypt(armorReader, identities...)
} else {
// Decrypt
ageReader, err = age.Decrypt(f, identities...)
}
if err != nil {
return "", err
// return errors.New("invalidKeyError")
}
// Read and decrypt data
out := &bytes.Buffer{}
if _, err := io.Copy(out, ageReader); err != nil {
return "", err
}
// Close File
f.Close()
// Sanitize Output
if len(outputPath) == 0 {
outputPath = GetHome() + string(filepath.Separator) + "age" + string(filepath.Separator) + "decrypted"
os.MkdirAll(outputPath, 0750)
}
outputPath = SanitizeOutput(outputPath, GetLastPartOfPath(inputPath))
// Remove .enc Suffix if needed
if strings.HasSuffix(outputPath, ".enc") {
outputPath = outputPath[:len(outputPath)-4]
}
// Save as file on disk
err = ioutil.WriteFile(outputPath, out.Bytes(), 0640)
if err != nil {
return "", errors.New("writeError%" + err.Error())
}
return outputPath, err
}