-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
136 lines (120 loc) · 3.02 KB
/
encrypt.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
/**
* This file is a part of the poni project and is licensed under the MIT license.
* See LICENSE.md for details.
*
* encrypt.go
* Contains the encryption command functions.
*
* @created 2023-08-23
*/
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"flag"
"fmt"
)
// encryptHandler
// Handles the encrypt command.
// Returns nothing.
func encryptHandler(args []string) {
var mode string
var key string
var plaintext string
var file string
var help bool
var armorOutput bool
var ciphertext []byte
var iv []byte
command := flag.NewFlagSet("encrypt", flag.ExitOnError)
command.StringVar(&key, "k", "", "The key to use for encryption")
command.StringVar(&plaintext, "m", "", "The plaintext to encrypt")
command.StringVar(&file, "f", "", "The file to encrypt")
command.BoolVar(&armorOutput, "a", false, "Whether to armor the output")
command.BoolVar(&help, "h", false, "Show the encryption help message")
err := command.Parse(args)
if err != nil {
return
}
if help {
encryptUsage()
return
}
// change mode based on present flags
if plaintext != "" {
mode = "plaintext"
} else if file != "" {
mode = "file"
} else {
mode = "stdin"
}
// check if key is present
if key == "" {
encryptUsage()
return
}
decodedKey, err := dearmor(key)
if err != nil {
fmt.Println("Failed to decode key")
return
}
switch mode {
case "plaintext":
ciphertext, iv, err = encrypt(decodedKey, []byte(plaintext))
case "file":
plaintext, err := readFile(file)
if err != nil {
fmt.Println("Failed to read file")
return
}
ciphertext, iv, err = encrypt(decodedKey, plaintext)
case "stdin":
plaintext, err := readStdin()
if err != nil {
fmt.Println("Failed to read stdin")
return
}
ciphertext, iv, err = encrypt(decodedKey, plaintext)
}
if err != nil {
fmt.Println("Failed to encrypt")
return
}
if armorOutput {
fmt.Println(armor(append(iv, ciphertext...)))
} else {
fmt.Println(append(iv, ciphertext...))
}
}
func encryptUsage() {
fmt.Println("Usage: poni encrypt -k <key> [-a armor] [-p <plaintext>] [-f <file>] [stdin]")
fmt.Println()
fmt.Print(` -k <key> The key to use for encryption
[-a] Armor the output using Base64 encoding
[-m <message>] A message provided in plain text to encrypt
[-f <file>] A file name whose contents will be encrypted
<nothing> By default, poni will encrypt whatever comes
through stdin`)
fmt.Println()
}
// encrypt
// Encrypts a plaintext using AES-GCM.
// Returns the ciphertext, the IV, and an error.
func encrypt(key []byte, plaintext []byte) (ciphertext []byte, iv []byte, err error) {
iv = make([]byte, 12)
_, err = rand.Read(iv)
if err != nil {
return nil, nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, err
}
AESCipher, err := cipher.NewGCM(block)
if err != nil {
return nil, nil, err
}
ciphertext = AESCipher.Seal(nil, iv, plaintext, nil)
return ciphertext, iv, nil
}