crypto_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package crypto
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/require"
  5. "testing"
  6. )
  7. func TestDeriveKey(t *testing.T) {
  8. key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
  9. require.Equal(t, "30b7e72f6273da6e59d2dec535466e548da3eafc98650c9664c06edab707fa25", fmt.Sprintf("%x", key))
  10. }
  11. func TestEncryptDecrypt(t *testing.T) {
  12. message := "this is a message or is it?"
  13. ciphertext, err := Encrypt([]byte(message), []byte("AES256Key-32Characters1234567890"))
  14. require.Nil(t, err)
  15. plaintext, err := Decrypt(ciphertext, []byte("AES256Key-32Characters1234567890"))
  16. require.Nil(t, err)
  17. require.Equal(t, message, string(plaintext))
  18. }
  19. func TestEncryptDecrypt_FromPHP(t *testing.T) {
  20. ciphertext := "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA"
  21. key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
  22. fmt.Printf("%x", key)
  23. plaintext, err := Decrypt(ciphertext, key)
  24. require.Nil(t, err)
  25. require.Equal(t, `{"message":"Secret!","priority":5}`, string(plaintext))
  26. }
  27. func TestEncryptDecrypt_FromPython(t *testing.T) {
  28. ciphertext := "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..gSRYZeX6eBhlj13w.LOchcxFXwALXE2GqdoSwFJEXdMyEbLfLKV9geXr17WrAN-nH7ya1VQ_Y6ebT1w.2eyLaTUfc_rpKaZr4-5I1Q"
  29. key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
  30. plaintext, err := Decrypt(ciphertext, key)
  31. require.Nil(t, err)
  32. require.Equal(t, `{"message":"Python says hi","tags":["secret"]}`, string(plaintext))
  33. }