diff --git a/util/cryp/cryp.go b/util/cryp/cryp.go deleted file mode 100644 index f920235f..00000000 --- a/util/cryp/cryp.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright © 2016 Abcum Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cryp - -import ( - "crypto/aes" - "crypto/cipher" - "errors" - "github.com/abcum/surreal/util/rand" -) - -func Encrypt(key []byte, src []byte) (dst []byte, err error) { - - if len(key) == 0 || len(src) == 0 { - return src, nil - } - - // Initiate AES - block, err := aes.NewCipher(key) - if err != nil { - return - } - - // Initiate cipher - cipher, _ := cipher.NewGCM(block) - - nonce := rand.New(12) - - dst = cipher.Seal(nil, nonce, src, nil) - - dst = append(nonce[:], dst[:]...) - - return - -} - -func Decrypt(key []byte, src []byte) (dst []byte, err error) { - - if len(key) == 0 || len(src) == 0 { - return src, nil - } - - // Corrupt - if len(src) < 12 { - return src, errors.New("Invalid data") - } - - // Initiate AES - block, err := aes.NewCipher(key) - if err != nil { - return - } - - // Initiate cipher - cipher, _ := cipher.NewGCM(block) - - return cipher.Open(nil, src[:12], src[12:], nil) - -} diff --git a/util/cryp/cryp_test.go b/util/cryp/cryp_test.go deleted file mode 100644 index 2dd89dcf..00000000 --- a/util/cryp/cryp_test.go +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright © 2016 Abcum Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cryp - -import ( - "testing" - - . "github.com/smartystreets/goconvey/convey" -) - -func TestEmpty(t *testing.T) { - - key := []byte("") - str := []byte("Hello World") - - 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 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 TestCorrupt(t *testing.T) { - - key := []byte("1hg7dbrma8ghe547") - enc := []byte("corrupt") - - Convey("Cryptography should fail", t, func() { - dec, err := Decrypt(key, enc) - Convey("Decrypt", func() { - So(err, ShouldNotBeNil) - So(dec, ShouldResemble, enc) - }) - }) - -} - -func TestInvalid(t *testing.T) { - - key := []byte("invalidkey") - str := []byte("Hello World") - - Convey("Cryptography should fail", t, func() { - enc, _ := Encrypt(key, str) - dec, _ := Decrypt(key, enc) - Convey("Encrypt", func() { - So(enc, ShouldResemble, []byte(nil)) - }) - Convey("Decrypt", func() { - So(dec, ShouldResemble, []byte(nil)) - }) - }) - -} - -func TestInvalidD(t *testing.T) { - - val := []byte("1hg7dbrma8ghe547") - key := []byte("invalidkey") - str := []byte("Hello World") - - Convey("Decryption should fail", t, func() { - enc, _ := Encrypt(val, str) - dec, _ := Decrypt(key, enc) - Convey("Decrypt", func() { - So(dec, ShouldResemble, []byte(nil)) - }) - }) - -} - -func TestInvalidE(t *testing.T) { - - key := []byte("invalidkey") - str := []byte("Hello World") - - Convey("Encryption should fail", t, func() { - enc, _ := Encrypt(key, str) - Convey("Encrypt", func() { - So(enc, ShouldResemble, []byte(nil)) - }) - }) - -} - -func TestAES128(t *testing.T) { - - key := []byte("1hg7dbrma8ghe547") - str := []byte("Hello World") - - Convey("AES-128 should encrypt and decrypt", t, func() { - enc, _ := Encrypt(key, str) - dec, _ := Decrypt(key, enc) - Convey("Encrypt", func() { - So(enc, ShouldNotResemble, str) - }) - Convey("Decrypt", func() { - So(dec, ShouldResemble, str) - }) - }) - -} - -func TestAES192(t *testing.T) { - - key := []byte("1hg7dbrma8ghe5473kghvie6") - str := []byte("Hello World") - - Convey("AES-192 should encrypt and decrypt", t, func() { - enc, _ := Encrypt(key, str) - dec, _ := Decrypt(key, enc) - Convey("Encrypt", func() { - So(enc, ShouldNotResemble, str) - }) - Convey("Decrypt", func() { - So(dec, ShouldResemble, str) - }) - }) - -} - -func TestAES256(t *testing.T) { - - key := []byte("1hg7dbrma8ghe5473kghvie64jgi3ph4") - str := []byte("Hello World") - - Convey("AES-256 should encrypt and decrypt", t, func() { - enc, _ := Encrypt(key, str) - dec, _ := Decrypt(key, enc) - Convey("Encrypt", func() { - So(enc, ShouldNotResemble, str) - }) - Convey("Decrypt", func() { - So(dec, ShouldResemble, str) - }) - }) - -}