E2E example in PHP and Python
This commit is contained in:
parent
99e6c0ff97
commit
67da1e4922
5 changed files with 113 additions and 117 deletions
|
@ -1,10 +1,7 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"github.com/stretchr/testify/require"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -14,40 +11,21 @@ func TestEncryptDecrypt(t *testing.T) {
|
|||
require.Nil(t, err)
|
||||
plaintext, err := Decrypt(ciphertext, []byte("AES256Key-32Characters1234567890"))
|
||||
require.Nil(t, err)
|
||||
log.Println(ciphertext)
|
||||
require.Equal(t, message, plaintext)
|
||||
}
|
||||
|
||||
func TestEncryptDecryptJWE(t *testing.T) {
|
||||
message := "this is a message or is it?"
|
||||
ciphertext, err := EncryptJWE(message, []byte("AES256Key-32Characters1234567890"))
|
||||
func TestEncryptDecrypt_FromPHP(t *testing.T) {
|
||||
ciphertext := "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA"
|
||||
key := DeriveKey("secr3t password", "https://ntfy.sh/mysecret")
|
||||
plaintext, err := Decrypt(ciphertext, key)
|
||||
require.Nil(t, err)
|
||||
plaintext, err := DecryptJWE(ciphertext, []byte("AES256Key-32Characters1234567890"))
|
||||
require.Nil(t, err)
|
||||
log.Println(ciphertext)
|
||||
require.Equal(t, message, plaintext)
|
||||
require.Equal(t, `{"message":"Secret!","priority":5}`, plaintext)
|
||||
}
|
||||
|
||||
func TestEncryptExpectedOutputxxxxx(t *testing.T) {
|
||||
// These values are taken from https://docs.pushbullet.com/#encryption
|
||||
// The following expected ciphertext from the site was used as a baseline:
|
||||
// MQS2K9l3G8YoLccJooY64kDeWjbkI3fAx4WcrYNtbz4p8Q==
|
||||
// 31 04b62bd9771bc6282dc709a2863ae240 de5a36e42377c0c7859cad83 6d6f3e29f1
|
||||
// v tag nonce ciphertext
|
||||
message := "meow!"
|
||||
key, _ := base64.StdEncoding.DecodeString("1sW28zp7CWv5TtGjlQpDHHG4Cbr9v36fG5o4f74LsKg=")
|
||||
nonce, _ := hex.DecodeString("de5a36e42377c0c7859cad83")
|
||||
ciphertext, err := encryptWithNonce(message, nonce, key)
|
||||
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)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "MQS2K9l3G8YoLccJooY64kDeWjbkI3fAx4WcrYNtbz4p8Q==", ciphertext)
|
||||
}
|
||||
|
||||
func TestEncryptExpectedOutput(t *testing.T) {
|
||||
// These values are taken from https://docs.pushbullet.com/#encryption, meaning that
|
||||
// all of this is compatible with how Pushbullet encrypts
|
||||
encryptedMessage := "MSfJxxY5YdjttlfUkCaKA57qU9SuCN8+ZhYg/xieI+lDnQ=="
|
||||
key, _ := base64.StdEncoding.DecodeString("1sW28zp7CWv5TtGjlQpDHHG4Cbr9v36fG5o4f74LsKg=")
|
||||
plaintext, err := Decrypt(encryptedMessage, key)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "meow!", plaintext)
|
||||
require.Equal(t, `{"message":"Python says hi","tags":["secret"]}`, plaintext)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue