-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpg.go
212 lines (178 loc) · 4.52 KB
/
gpg.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
"golang.org/x/crypto/ssh/terminal"
)
const Description = `
A tool to simplify gpg
simple-gpg [args] file
Flags:
`
const Examples = `
Examples:
# To encrypt a file
$ simple-gpg accounts.pdf
Enter password:
# To encrypt a file with passowrd as argument
$ simple-gpg -password secret-word accounts.pdf
# To decrypt a file
$ simple-gpg -decrypt accounts.pdf
# To encrypt a folder
$ simple-gpg /path/to/folder
Note: When encrypting a folder, simple-gpg uses tar.gz format to compress the folder into an archive and then encrypts the archive
`
func EncrpytFile(file string, password []byte, algo string, outputFile string) error {
cipher := packet.CipherAES256
if algo == "AES" {
cipher = packet.CipherAES128
} else if algo == "AES192" {
cipher = packet.CipherAES192
} else if algo != "AES256" {
fmt.Println("Unknown cipher", algo, "provided. Using AES256 as default")
}
config := &packet.Config{
DefaultCipher: cipher,
}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
w, err := os.Create(outputFile)
if err != nil {
return err
}
defer w.Close()
fmt.Println("Encrypting:", file)
pt, err := openpgp.SymmetricallyEncrypt(w, password, nil, config)
if _, err := io.Copy(pt, f); err != nil {
return err
}
fmt.Println("Encryption successful:", outputFile)
return pt.Close()
}
func tarDir(srcPath, dstPath string) error {
f, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
panic(fmt.Sprintf("Error creating file %v", err.Error()))
}
return Tar(srcPath, f)
}
func IsDir(path string) bool {
if info, err := os.Stat(path); err == nil && !info.IsDir() {
return false
}
return true
}
func randomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func compressDir(path string) (string, error) {
// Tar + gzip this
fmt.Printf("WARNING: %s is a directory. Converting into .tar.gz\n", path)
outputPath := filepath.Join("/tmp/", filepath.Base(path)+"-"+randomString(12))
err := tarDir(path, outputPath)
return outputPath, err
}
func DecrpytFile(file string, password []byte) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
failed := false
prompt := func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
if failed {
return nil, errors.New("Decryption failed. Check password")
}
failed = true
return password, nil
}
fmt.Println("Decrypting:", file)
md, err := openpgp.ReadMessage(f, nil, prompt, &packet.Config{})
if err != nil {
return err
}
plaintext, err := ioutil.ReadAll(md.UnverifiedBody)
if err != nil {
return err
}
path := fmt.Sprintf("decrypted-%s", strings.TrimSuffix(file, ".gpg"))
dst, err := os.Create(path)
if err != nil {
return err
}
defer dst.Close()
dst.Write(plaintext)
fmt.Println("Decryption successful:", path)
return nil
}
func main() {
algo := flag.String("cipher-algo", "AES256", "Cipher algorithm to be used. Choose one of AES, AES192, AES256")
decrypt := flag.Bool("decrypt", false, "Set to true if you want to decrypt the file")
cliPassword := flag.String("password", "", "Password to use when encrypting/decrypting")
outputFile := flag.String("outputFile", "", "Path for output file. (Default: <filename>.gpg)")
flag.Usage = func() {
fmt.Println(Description)
flag.PrintDefaults()
fmt.Println(Examples)
}
flag.Parse()
var err error
if len(flag.Args()) != 1 {
fmt.Println("Error: Unexpected number of arguments")
flag.Usage()
os.Exit(1)
}
file := flag.Args()[0]
var password []byte
if *cliPassword == "" {
fmt.Printf("Enter password: ")
password, err = terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
fmt.Println()
} else {
password = []byte(*cliPassword)
}
if *decrypt {
err = DecrpytFile(file, password)
} else {
outputPath := *outputFile
if IsDir(file) {
if outputPath == "" {
outputPath = file + ".tar.gz.gpg"
}
compressedDirPath, err := compressDir(file)
if err != nil {
panic(err)
}
err = EncrpytFile(compressedDirPath, password, *algo, outputPath)
os.Remove(compressedDirPath)
} else {
if outputPath == "" {
outputPath = file + ".gpg"
}
err = EncrpytFile(file, password, *algo, outputPath)
}
}
if err != nil {
panic(err)
}
}