-
Notifications
You must be signed in to change notification settings - Fork 64
/
crypt.go
161 lines (148 loc) Β· 3.14 KB
/
crypt.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
package cmd
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"github.com/charmbracelet/charm/crypt"
"github.com/spf13/cobra"
)
var (
// CryptCmd is the cobra.Command to manage encryption and decryption for a user.
CryptCmd = &cobra.Command{
Use: "crypt",
Hidden: false,
Short: "Use Charm encryption.",
Long: styles.Paragraph.Render("Commands to encrypt and decrypt data with your Charm Cloud encryption keys."),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
cryptEncryptCmd = &cobra.Command{
Use: "encrypt",
Hidden: false,
Short: "Encrypt stdin with your Charm account encryption key",
Args: cobra.NoArgs,
RunE: cryptEncrypt,
}
cryptDecryptCmd = &cobra.Command{
Use: "decrypt",
Hidden: false,
Short: "Decrypt stdin with your Charm account encryption key",
Args: cobra.RangeArgs(0, 1),
RunE: cryptDecrypt,
}
cryptEncryptLookupCmd = &cobra.Command{
Use: "encrypt-lookup",
Hidden: false,
Short: "Encrypt arg deterministically",
Args: cobra.ExactArgs(1),
RunE: cryptEncryptLookup,
}
cryptDecryptLookupCmd = &cobra.Command{
Use: "decrypt-lookup",
Hidden: false,
Short: "Decrypt arg deterministically",
Args: cobra.ExactArgs(1),
RunE: cryptDecryptLookup,
}
)
type cryptFile struct {
Data string `json:"data"`
}
func cryptEncrypt(cmd *cobra.Command, args []string) error {
cr, err := crypt.NewCrypt()
if err != nil {
return err
}
buf := bytes.NewBuffer(nil)
eb, err := cr.NewEncryptedWriter(buf)
if err != nil {
return err
}
_, err = io.Copy(eb, os.Stdin)
if err != nil {
return err
}
eb.Close() // nolint:errcheck
cf := cryptFile{
Data: base64.StdEncoding.EncodeToString(buf.Bytes()),
}
out, err := json.Marshal(cf)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
func cryptDecrypt(cmd *cobra.Command, args []string) error {
var r io.Reader
cr, err := crypt.NewCrypt()
if err != nil {
return err
}
switch len(args) {
case 0:
r = os.Stdin
default:
f, err := os.Open(args[0])
defer f.Close() // nolint:errcheck
r = f
if err != nil {
return err
}
}
cf := &cryptFile{}
jd := json.NewDecoder(r)
err = jd.Decode(cf)
if err != nil {
return err
}
d, err := base64.StdEncoding.DecodeString(cf.Data)
if err != nil {
return err
}
br := bytes.NewReader(d)
deb, err := cr.NewDecryptedReader(br)
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, deb)
if err != nil {
return err
}
return nil
}
func cryptEncryptLookup(cmd *cobra.Command, args []string) error {
cr, err := crypt.NewCrypt()
if err != nil {
return err
}
ct, err := cr.EncryptLookupField(args[0])
if err != nil {
return err
}
fmt.Println(ct)
return nil
}
func cryptDecryptLookup(cmd *cobra.Command, args []string) error {
cr, err := crypt.NewCrypt()
if err != nil {
return err
}
pt, err := cr.DecryptLookupField(args[0])
if err != nil {
return err
}
fmt.Println(pt)
return nil
}
func init() {
CryptCmd.AddCommand(cryptEncryptCmd)
CryptCmd.AddCommand(cryptDecryptCmd)
CryptCmd.AddCommand(cryptEncryptLookupCmd)
CryptCmd.AddCommand(cryptDecryptLookupCmd)
}