mirror of
https://github.com/datarhei/core.git
synced 2025-10-06 00:17:07 +08:00

This secret will be used to encrypt automatically obtained secrets at rest, i.e. in a storage. They will be decrypted on demand. If the secret is wrong, stored certificates can't be decrypted. For changing the secret, the stored certificated must be deleted first in order to obtain new ones that will be encrypted with the new secret.
40 lines
853 B
Go
40 lines
853 B
Go
package autocert
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
|
c := NewCrypto("foobar")
|
|
|
|
data := "top secret"
|
|
|
|
encryptedData, err := c.Encrypt([]byte(data))
|
|
require.NoError(t, err)
|
|
require.NotEqual(t, []byte(data), encryptedData)
|
|
|
|
decryptedData, err := c.Decrypt(encryptedData)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []byte(data), decryptedData)
|
|
}
|
|
|
|
func TestEncryptDecryptWrongSecret(t *testing.T) {
|
|
c1 := NewCrypto("foobar")
|
|
c2 := NewCrypto("foobaz")
|
|
|
|
data := "top secret"
|
|
|
|
encryptedData, err := c1.Encrypt([]byte(data))
|
|
require.NoError(t, err)
|
|
require.NotEqual(t, []byte(data), encryptedData)
|
|
|
|
_, err = c2.Decrypt(encryptedData)
|
|
require.Error(t, err)
|
|
|
|
decryptedData, err := c1.Decrypt(encryptedData)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []byte(data), decryptedData)
|
|
}
|