[ISSUE-67869881] address comments
diff --git a/cipher/cipher.go b/cipher/cipher.go
index 06d8242..e560d71 100644
--- a/cipher/cipher.go
+++ b/cipher/cipher.go
@@ -17,7 +17,6 @@
"crypto/aes"
"crypto/cipher"
"fmt"
- "sync"
)
type Mode string
@@ -72,28 +71,17 @@
// Create a new AesCipher object with the specified encryption mode and padding algorithm.
func CreateAesCipher(key []byte) (*AesCipher, error) {
a := &AesCipher{
- mutex: &sync.RWMutex{},
+ key: key,
}
- if err := a.SetKey(key); err != nil {
- return nil, err
- }
- return a, nil
+ var err error
+ a.block, err = aes.NewCipher(key)
+ return a, err
}
// An object to perform AES encryption/decryption.
type AesCipher struct {
key []byte
block cipher.Block
- mutex *sync.RWMutex
-}
-
-// Set/Change the AES key, accepted key's bit-size is 128/192/256.
-func (a *AesCipher) SetKey(key []byte) (err error) {
- a.mutex.Lock()
- defer a.mutex.Unlock()
- a.key = key
- a.block, err = aes.NewCipher(key)
- return err
}
// Encrypt the plaintext. Padding is performed before encryption, so the
@@ -116,15 +104,11 @@
ciphertext = text
// encrypt
- a.mutex.RLock()
- block := a.block
- a.mutex.RUnlock()
-
switch mode {
case ModeEcb:
- size := block.BlockSize()
+ size := a.block.BlockSize()
for len(text) > 0 {
- block.Encrypt(text, text)
+ a.block.Encrypt(text, text)
text = text[size:]
}
}
@@ -148,9 +132,7 @@
}
// encrypt
- a.mutex.RLock()
block := a.block
- a.mutex.RUnlock()
switch mode {
case ModeEcb:
diff --git a/cipher/cipher_test.go b/cipher/cipher_test.go
index 0bdde75..0f999a7 100644
--- a/cipher/cipher_test.go
+++ b/cipher/cipher_test.go
@@ -83,18 +83,6 @@
})
})
- It("SetKey", func() {
- key := make([]byte, 16)
- plaintext := []byte("aUWQKgAwmaR0p2kY")
- ciphertext := []byte{218, 53, 247, 87, 119, 80, 231, 16, 125, 11, 214, 101, 246, 202, 178, 163, 202, 102, 146, 245, 79, 215, 74, 228, 17, 83, 213, 134, 105, 203, 31, 14}
- c, err := cipher.CreateAesCipher(key)
- Expect(err).Should(Succeed())
- key = []byte{2, 122, 212, 83, 150, 164, 180, 4, 148, 242, 65, 189, 3, 188, 76, 247}
- Expect(c.SetKey(key)).Should(Succeed())
- Expect(c.Encrypt(plaintext, cipher.ModeEcb, cipher.PaddingPKCS5)).Should(Equal(ciphertext))
- Expect(c.Decrypt(ciphertext, cipher.ModeEcb, cipher.PaddingPKCS7)).Should(Equal(plaintext))
- })
-
It("Invalid Parameters", func() {
_, err := cipher.CreateAesCipher(make([]byte, 15))
Expect(err).ToNot(Succeed())