-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
69 lines (58 loc) · 1.43 KB
/
main.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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"os"
"path"
)
var inFilePath string
var outFilePath string
var pubKeyPath string
var privKeyPath string
var doDecrypt bool
func init() {
flag.StringVar(&inFilePath, "in", "-", "Input file")
flag.StringVar(&outFilePath, "out", "-", "Output file")
flag.StringVar(&pubKeyPath, "pubKey", path.Join(os.Getenv("HOME"), ".ssh/id_rsa.pub"), "Public Key file (Encrypting)")
flag.StringVar(&privKeyPath, "privKey", path.Join(os.Getenv("HOME"), ".ssh/id_rsa"), "Private Key file (Decrypting)")
flag.BoolVar(&doDecrypt, "d", false, "Decrypt file instead of encrypting")
}
func main() {
flag.Parse()
var err error
var inFile *os.File
if inFilePath == "-" {
inFile = os.Stdin
} else {
inFile, err = os.Open(inFilePath)
if err != nil {
panic(err)
}
}
defer inFile.Close()
var outFile *os.File
if outFilePath == "-" {
outFile = os.Stdout
} else {
outFile, err = os.OpenFile("encrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(err)
}
}
defer outFile.Close()
if doDecrypt {
privKey, err := openPrivKey(privKeyPath)
if err != nil {
panic(err)
}
gpgDecrypt(privKey, inFile, outFile)
return
}
pubKey, err := openPubKey(pubKeyPath)
if err != nil {
panic(err)
}
gpgEncrypt(pubKey, inFile, outFile)
}