Encrypt passwords in DB (#1187)
* Encode/Encrypt passwords in DB * Only decrypts passwords if it is necessary * Add tests for encryption functions
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("encrypt", func() {
|
||||
It("decrypts correctly when using the same encryption key", func() {
|
||||
sum := sha256.Sum256([]byte("password"))
|
||||
encKey := sum[0:]
|
||||
data := "Can you keep a secret?"
|
||||
|
||||
encrypted, err := Encrypt(context.Background(), encKey, data)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
decrypted, err := Decrypt(context.Background(), encKey, encrypted)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(decrypted).To(Equal(data))
|
||||
})
|
||||
|
||||
It("fails to decrypt if not using the same encryption key", func() {
|
||||
sum := sha256.Sum256([]byte("password"))
|
||||
encKey := sum[0:]
|
||||
data := "Can you keep a secret?"
|
||||
|
||||
encrypted, err := Encrypt(context.Background(), encKey, data)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
sum = sha256.Sum256([]byte("different password"))
|
||||
encKey = sum[0:]
|
||||
_, err = Decrypt(context.Background(), encKey, encrypted)
|
||||
Expect(err).To(MatchError("cipher: message authentication failed"))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user