From e2e691d9c23d2b18b032f4d464ca0756c134ed87 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 13 Mar 2020 23:53:15 -0700 Subject: [PATCH] clean up, add test --- weed/util/cipher.go | 21 --------------------- weed/util/cipher_test.go | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 21 deletions(-) create mode 100644 weed/util/cipher_test.go diff --git a/weed/util/cipher.go b/weed/util/cipher.go index 7bcb6559a..f044c2ca3 100644 --- a/weed/util/cipher.go +++ b/weed/util/cipher.go @@ -1,14 +1,11 @@ package util import ( - "bytes" "crypto/aes" "crypto/cipher" "crypto/rand" "errors" - "fmt" "io" - "io/ioutil" "github.com/chrislusf/seaweedfs/weed/glog" ) @@ -61,21 +58,3 @@ func Decrypt(ciphertext []byte, key CipherKey) ([]byte, error) { nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] return gcm.Open(nil, nonce, ciphertext, nil) } - -func EncryptReader(clearReader io.Reader) (cipherKey CipherKey, encryptedReader io.ReadCloser, clearDataLen, encryptedDataLen int, err error) { - clearData, err := ioutil.ReadAll(clearReader) - if err != nil { - err = fmt.Errorf("read raw input: %v", err) - return - } - clearDataLen = len(clearData) - cipherKey = GenCipherKey() - encryptedData, err := Encrypt(clearData, cipherKey) - if err != nil { - err = fmt.Errorf("encrypt input: %v", err) - return - } - encryptedDataLen = len(encryptedData) - encryptedReader = ioutil.NopCloser(bytes.NewReader(encryptedData)) - return -} diff --git a/weed/util/cipher_test.go b/weed/util/cipher_test.go new file mode 100644 index 000000000..026c96ea3 --- /dev/null +++ b/weed/util/cipher_test.go @@ -0,0 +1,17 @@ +package util + +import ( + "encoding/base64" + "testing" +) + +func TestSameAsJavaImplementation(t *testing.T) { + str := "QVVhmqg112NMT7F+G/7QPynqSln3xPIhKdFGmTVKZD6IS0noyr2Z5kXFF6fPjZ/7Hq8kRhlmLeeqZUccxyaZHezOdgkjS6d4NTdHf5IjXzk7" + cipherText, _ := base64.StdEncoding.DecodeString(str) + secretKey := []byte("256-bit key for AES 256 GCM encr") + plantext, err := Decrypt(cipherText, CipherKey(secretKey)) + if err != nil { + println(err.Error()) + } + println(string(plantext)) +}