-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.go
More file actions
129 lines (107 loc) · 2.59 KB
/
Copy pathcrypt.go
File metadata and controls
129 lines (107 loc) · 2.59 KB
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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"errors"
"fmt"
"os"
"strings"
"syscall"
"github.com/alexflint/go-arg"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/term"
)
var salt = []byte{
56, 78, 185, 170, 79, 135, 36, 227,
37, 110, 177, 104, 178, 158, 197, 146,
}
func Main() error {
var args struct {
Encrypt []string `placeholder:"PATH"`
Decrypt []string `placeholder:"PATH"`
}
p := arg.MustParse(&args)
if len(args.Encrypt) == 0 && len(args.Decrypt) == 0 {
p.Fail("you must provide one of --encrypt or --decrypt")
}
// prompt for password
fmt.Print("Enter password: ")
password, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return fmt.Errorf("error reading password: %w", err)
}
fmt.Println()
// derive a key
key := pbkdf2.Key(password, salt, 4096, 32, sha1.New)
// process paths
for _, path := range args.Encrypt {
err := process(path, key, encrypt)
if err != nil {
return err
}
}
for _, path := range args.Decrypt {
err := process(path, key, decrypt)
if err != nil {
return err
}
}
return nil
}
func process(path string, key []byte, f func([]byte, []byte) ([]byte, error)) error {
in, err := os.ReadFile(path)
if err != nil {
return err
}
out, err := f(in, key)
if err != nil {
return err
}
var outpath string
if strings.HasSuffix(path, ".encrypted") {
outpath = strings.TrimSuffix(path, ".encrypted")
} else {
outpath = path + ".encrypted"
}
return os.WriteFile(outpath, out, os.ModePerm)
}
func encrypt(plaintext []byte, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
// Wet nonce to all zeros so that the ciphertext is deterministic given
// the plaintext and the password. We are not concerned about replay
// attacks or recognition attacks because the ciphertext is not used for
// any kind of authentication, nor sent over any unencrypted channels
// under any conditions.
nonce := make([]byte, gcm.NonceSize())
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
func decrypt(ciphertext []byte, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
return gcm.Open(nil, nonce, ciphertext, nil)
}
func main() {
if err := Main(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}