Don't encrypt/decrypt blank strings

This commit is contained in:
Tobie Morgan Hitchcock 2016-07-18 14:24:48 +01:00
parent 330d834706
commit 3c8b68b503
2 changed files with 20 additions and 2 deletions

View file

@ -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
}

View file

@ -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")