2022-07-01 21:48:49 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2022-07-14 02:31:17 +02:00
|
|
|
"fmt"
|
2022-07-01 21:48:49 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-07-14 02:31:17 +02:00
|
|
|
func TestDeriveKey(t *testing.T) {
|
|
|
|
key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
|
|
|
|
require.Equal(t, "30b7e72f6273da6e59d2dec535466e548da3eafc98650c9664c06edab707fa25", fmt.Sprintf("%x", key))
|
|
|
|
}
|
|
|
|
|
2022-07-01 21:48:49 +02:00
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
|
|
|
message := "this is a message or is it?"
|
2022-07-14 02:31:17 +02:00
|
|
|
ciphertext, err := Encrypt([]byte(message), []byte("AES256Key-32Characters1234567890"))
|
2022-07-01 21:48:49 +02:00
|
|
|
require.Nil(t, err)
|
|
|
|
plaintext, err := Decrypt(ciphertext, []byte("AES256Key-32Characters1234567890"))
|
|
|
|
require.Nil(t, err)
|
2022-07-14 02:31:17 +02:00
|
|
|
require.Equal(t, message, string(plaintext))
|
2022-07-01 21:48:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 04:58:43 +02:00
|
|
|
func TestEncryptDecrypt_FromPHP(t *testing.T) {
|
|
|
|
ciphertext := "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA"
|
|
|
|
key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
|
2022-07-14 02:31:17 +02:00
|
|
|
fmt.Printf("%x", key)
|
2022-07-06 04:58:43 +02:00
|
|
|
plaintext, err := Decrypt(ciphertext, key)
|
2022-07-01 21:48:49 +02:00
|
|
|
require.Nil(t, err)
|
2022-07-14 02:31:17 +02:00
|
|
|
require.Equal(t, `{"message":"Secret!","priority":5}`, string(plaintext))
|
2022-07-01 21:48:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 04:58:43 +02:00
|
|
|
func TestEncryptDecrypt_FromPython(t *testing.T) {
|
|
|
|
ciphertext := "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..gSRYZeX6eBhlj13w.LOchcxFXwALXE2GqdoSwFJEXdMyEbLfLKV9geXr17WrAN-nH7ya1VQ_Y6ebT1w.2eyLaTUfc_rpKaZr4-5I1Q"
|
|
|
|
key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
|
|
|
|
plaintext, err := Decrypt(ciphertext, key)
|
2022-07-01 21:48:49 +02:00
|
|
|
require.Nil(t, err)
|
2022-07-14 02:31:17 +02:00
|
|
|
require.Equal(t, `{"message":"Python says hi","tags":["secret"]}`, string(plaintext))
|
2022-07-01 21:48:49 +02:00
|
|
|
}
|