From 9223fc79e9d337cc9a5a7034904cef5c2740fb4c Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 29 Apr 2019 19:19:25 -0700 Subject: [PATCH] allow option to skip encryption --- src/crypt/crypt.go | 13 +++++++++++++ src/crypt/crypt_test.go | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/crypt/crypt.go b/src/crypt/crypt.go index b64b9bc..743eb91 100644 --- a/src/crypt/crypt.go +++ b/src/crypt/crypt.go @@ -17,7 +17,12 @@ type Encryption struct { // New generates a new Encryption, using the supplied passphrase and // an optional supplied salt. +// Passing nil passphrase will not use decryption. func New(passphrase []byte, salt []byte) (e Encryption, err error) { + if passphrase == nil { + e = Encryption{nil, nil, nil} + return + } e.passphrase = passphrase if salt == nil { e.salt = make([]byte, 8) @@ -37,6 +42,10 @@ func (e Encryption) Salt() []byte { // Encrypt will generate an Encryption, prefixed with the IV func (e Encryption) Encrypt(plaintext []byte) (encrypted []byte, err error) { + if e.passphrase == nil { + encrypted = plaintext + return + } // generate a random iv each time // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf // Section 8.2 @@ -57,6 +66,10 @@ func (e Encryption) Encrypt(plaintext []byte) (encrypted []byte, err error) { // Decrypt an Encryption func (e Encryption) Decrypt(encrypted []byte) (plaintext []byte, err error) { + if e.passphrase == nil { + plaintext = encrypted + return + } b, err := aes.NewCipher(e.key) if err != nil { return diff --git a/src/crypt/crypt_test.go b/src/crypt/crypt_test.go index 2378601..f4fc6b9 100644 --- a/src/crypt/crypt_test.go +++ b/src/crypt/crypt_test.go @@ -44,3 +44,18 @@ func TestEncryption(t *testing.T) { assert.NotEqual(t, dec, []byte("hello, world")) } + + +func TestNoEncryption(t *testing.T) { + bob, err := New(nil, nil) + assert.Nil(t, err) + jane, err := New(nil,nil) + assert.Nil(t, err) + enc, err := bob.Encrypt([]byte("hello, world")) + assert.Nil(t, err) + dec, err := jane.Decrypt(enc) + assert.Nil(t, err) + assert.Equal(t, dec, []byte("hello, world")) + assert.Equal(t, enc, []byte("hello, world")) + +} \ No newline at end of file