From 98399d8518849cf7d531ec9fe56c517237fb278b Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Fri, 8 Mar 2019 10:23:34 +0100 Subject: [PATCH] cleanup --- main.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index b8d04e2..5ff9cf3 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "crypto/cipher" "crypto/rand" "errors" + "fmt" "io" mrand "math/rand" "time" @@ -15,7 +16,7 @@ const ( allowedRunes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) -// RandomKey returns a randomly generated 32 bytes long hex encoded key. +// RandomKey returns a randomly generated 32 bytes long key. func RandomKey() (key []byte) { mrand.Seed(time.Now().UnixNano()) @@ -29,7 +30,11 @@ func RandomKey() (key []byte) { } // Encrypt encrypts plaintext with key and returns resulting bytes. -func Encrypt(plaintext []byte, key []byte) ([]byte, error) { +func Encrypt(plaintext, key []byte) ([]byte, error) { + if len(key) != keySize { + return nil, fmt.Errorf("key size invalid: %d, must be %d\n", len(key), keySize) + } + c, err := aes.NewCipher(key) if err != nil { return nil, err @@ -49,7 +54,7 @@ func Encrypt(plaintext []byte, key []byte) ([]byte, error) { } // Decrypt decrypts ciphertext with key and returns resulting bytes. -func Decrypt(ciphertext []byte, key []byte) ([]byte, error) { +func Decrypt(ciphertext, key []byte) ([]byte, error) { c, err := aes.NewCipher(key) if err != nil { return nil, err @@ -66,5 +71,6 @@ func Decrypt(ciphertext []byte, key []byte) ([]byte, error) { } nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] + return gcm.Open(nil, nonce, ciphertext, nil) } -- 1.8.1.2