Commit 98399d8518849cf7d531ec9fe56c517237fb278b
1 parent
74f41102fa
Exists in
master
cleanup
Showing
1 changed file
with
9 additions
and
3 deletions
Show diff stats
main.go
... | ... | @@ -5,6 +5,7 @@ import ( |
5 | 5 | "crypto/cipher" |
6 | 6 | "crypto/rand" |
7 | 7 | "errors" |
8 | + "fmt" | |
8 | 9 | "io" |
9 | 10 | mrand "math/rand" |
10 | 11 | "time" |
... | ... | @@ -15,7 +16,7 @@ const ( |
15 | 16 | allowedRunes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
16 | 17 | ) |
17 | 18 | |
18 | -// RandomKey returns a randomly generated 32 bytes long hex encoded key. | |
19 | +// RandomKey returns a randomly generated 32 bytes long key. | |
19 | 20 | func RandomKey() (key []byte) { |
20 | 21 | mrand.Seed(time.Now().UnixNano()) |
21 | 22 | |
... | ... | @@ -29,7 +30,11 @@ func RandomKey() (key []byte) { |
29 | 30 | } |
30 | 31 | |
31 | 32 | // Encrypt encrypts plaintext with key and returns resulting bytes. |
32 | -func Encrypt(plaintext []byte, key []byte) ([]byte, error) { | |
33 | +func Encrypt(plaintext, key []byte) ([]byte, error) { | |
34 | + if len(key) != keySize { | |
35 | + return nil, fmt.Errorf("key size invalid: %d, must be %d\n", len(key), keySize) | |
36 | + } | |
37 | + | |
33 | 38 | c, err := aes.NewCipher(key) |
34 | 39 | if err != nil { |
35 | 40 | return nil, err |
... | ... | @@ -49,7 +54,7 @@ func Encrypt(plaintext []byte, key []byte) ([]byte, error) { |
49 | 54 | } |
50 | 55 | |
51 | 56 | // Decrypt decrypts ciphertext with key and returns resulting bytes. |
52 | -func Decrypt(ciphertext []byte, key []byte) ([]byte, error) { | |
57 | +func Decrypt(ciphertext, key []byte) ([]byte, error) { | |
53 | 58 | c, err := aes.NewCipher(key) |
54 | 59 | if err != nil { |
55 | 60 | return nil, err |
... | ... | @@ -66,5 +71,6 @@ func Decrypt(ciphertext []byte, key []byte) ([]byte, error) { |
66 | 71 | } |
67 | 72 | |
68 | 73 | nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] |
74 | + | |
69 | 75 | return gcm.Open(nil, nonce, ciphertext, nil) |
70 | 76 | } | ... | ... |