Skip to content

Commit b8d72a6

Browse files
committed
Add encrypt and decrypt helper cmd
1 parent dcdca1f commit b8d72a6

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

pkg/cli/encrypt.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/convox/rack/pkg/crypt"
7+
"github.com/convox/rack/sdk"
8+
"github.com/convox/stdcli"
9+
)
10+
11+
func init() {
12+
register("encrypt", "encrypt data using key", Encrypt, stdcli.CommandOptions{
13+
Flags: []stdcli.Flag{
14+
stdcli.StringFlag("key", "", "key"),
15+
stdcli.StringFlag("data", "", "data"),
16+
},
17+
Usage: "",
18+
})
19+
20+
register("decrypt", "decrypt data using key", Decrypt, stdcli.CommandOptions{
21+
Flags: []stdcli.Flag{
22+
stdcli.StringFlag("key", "", "key"),
23+
stdcli.StringFlag("data", "", "data"),
24+
},
25+
Usage: "",
26+
})
27+
}
28+
29+
func Encrypt(_ sdk.Interface, c *stdcli.Context) error {
30+
31+
key := c.String("key")
32+
data := c.String("data")
33+
34+
if key == "" || data == "" {
35+
return fmt.Errorf("key and data must be non empty")
36+
}
37+
38+
val, err := crypt.Encrypt(key, []byte(data))
39+
if err != nil {
40+
return err
41+
}
42+
43+
fmt.Println(val)
44+
return nil
45+
}
46+
47+
func Decrypt(_ sdk.Interface, c *stdcli.Context) error {
48+
key := c.String("key")
49+
data := c.String("data")
50+
51+
if key == "" || data == "" {
52+
return fmt.Errorf("key and data must be non empty")
53+
}
54+
55+
val, err := crypt.Decrypt(key, data)
56+
if err != nil {
57+
return err
58+
}
59+
60+
fmt.Println(string(val))
61+
return nil
62+
}

pkg/crypt/helpers.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package crypt
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/sha256"
6+
"encoding/base64"
7+
"encoding/json"
8+
"fmt"
9+
"io"
10+
11+
"github.com/pkg/errors"
12+
"golang.org/x/crypto/nacl/secretbox"
13+
)
14+
15+
const (
16+
KeySize = 32
17+
NonceSize = 24
18+
)
19+
20+
type RandEnvelope struct {
21+
Nonce *[NonceSize]byte
22+
CipherText []byte
23+
}
24+
25+
func OneWay(str string) string {
26+
enc := sha256.Sum256([]byte(str))
27+
return base64.StdEncoding.EncodeToString(enc[:])
28+
}
29+
30+
func Encrypt(ekey string, data []byte) (string, error) {
31+
key, err := decodeKey(ekey)
32+
if err != nil {
33+
return "", errors.WithStack(err)
34+
}
35+
36+
nonce, err := generateNonce()
37+
if err != nil {
38+
return "", errors.WithStack(err)
39+
}
40+
41+
var cipherText []byte
42+
cipherText = secretbox.Seal(cipherText, data, nonce, key)
43+
44+
envelope := RandEnvelope{
45+
Nonce: nonce,
46+
CipherText: cipherText,
47+
}
48+
49+
envelopeJson, err := json.Marshal(envelope)
50+
if err != nil {
51+
return "", errors.WithStack(err)
52+
}
53+
54+
return base64.StdEncoding.EncodeToString(envelopeJson), nil
55+
}
56+
57+
func Decrypt(ekey string, sealed string) ([]byte, error) {
58+
decoded, err := base64.StdEncoding.DecodeString(sealed)
59+
if err != nil {
60+
return nil, errors.WithStack(err)
61+
}
62+
63+
var envelope RandEnvelope
64+
65+
if err := json.Unmarshal(decoded, &envelope); err != nil {
66+
return nil, errors.WithStack(err)
67+
}
68+
69+
key, err := decodeKey(ekey)
70+
if err != nil {
71+
return nil, errors.WithStack(err)
72+
}
73+
74+
data := []byte{}
75+
76+
data, ok := secretbox.Open(nil, envelope.CipherText, envelope.Nonce, key)
77+
if !ok {
78+
return nil, errors.WithStack(fmt.Errorf("could not decrypt data"))
79+
}
80+
81+
return data, nil
82+
}
83+
84+
func decodeKey(ekey string) (*[KeySize]byte, error) {
85+
var key [KeySize]byte
86+
87+
data, err := base64.StdEncoding.DecodeString(ekey)
88+
if err != nil {
89+
return nil, errors.WithStack(err)
90+
}
91+
92+
copy(key[:], data[0:KeySize])
93+
94+
return &key, nil
95+
}
96+
97+
func generateNonce() (*[NonceSize]byte, error) {
98+
nonce := new([NonceSize]byte)
99+
_, err := io.ReadFull(rand.Reader, nonce[:])
100+
if err != nil {
101+
return nil, errors.WithStack(err)
102+
}
103+
104+
return nonce, nil
105+
}

0 commit comments

Comments
 (0)