From 3c8b68b503968e78299e2c1c5495518b93fe8f6f Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 18 Jul 2016 14:24:48 +0100 Subject: [PATCH] Don't encrypt/decrypt blank strings --- util/cryp/cryp.go | 4 ++-- util/cryp/cryp_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/util/cryp/cryp.go b/util/cryp/cryp.go index 979713b8..0c39c083 100644 --- a/util/cryp/cryp.go +++ b/util/cryp/cryp.go @@ -22,7 +22,7 @@ import ( func Encrypt(key []byte, src []byte) (dst []byte, err error) { - if len(key) == 0 { + if len(key) == 0 || len(src) == 0 { return src, nil } @@ -50,7 +50,7 @@ func Encrypt(key []byte, src []byte) (dst []byte, err error) { func Decrypt(key []byte, src []byte) (dst []byte, err error) { - if len(key) == 0 { + if len(key) == 0 || len(src) == 0 { return src, nil } diff --git a/util/cryp/cryp_test.go b/util/cryp/cryp_test.go index 862b2f3f..f9c13c2f 100644 --- a/util/cryp/cryp_test.go +++ b/util/cryp/cryp_test.go @@ -38,6 +38,24 @@ func TestEmpty(t *testing.T) { } +func TestBlank(t *testing.T) { + + key := []byte("1hg7dbrma8ghe547") + str := []byte("") + + Convey("Cryptography should fail", t, func() { + enc, _ := Encrypt(key, str) + dec, _ := Decrypt(key, enc) + Convey("Encrypt", func() { + So(enc, ShouldResemble, str) + }) + Convey("Decrypt", func() { + So(dec, ShouldResemble, str) + }) + }) + +} + func TestInvalid(t *testing.T) { key := []byte("invalidkey")