decred.org/dcrdex@v1.0.5/tatanka/client/client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"crypto/rsa"
     7  	"testing"
     8  )
     9  
    10  func TestEncryption(t *testing.T) {
    11  	priv, err := rsa.GenerateKey(rand.Reader, rsaPrivateKeyLength)
    12  	if err != nil {
    13  		t.Fatalf("error generating rsa key: %v", err)
    14  	}
    15  	p := &peer{
    16  		encryptionKey: &priv.PublicKey,
    17  		decryptionKey: priv,
    18  	}
    19  
    20  	msg := []byte(
    21  		"this is an unencrypted message. " +
    22  			"For a size 2048 private key -> 256 byte public key, it needs to be longer than 190 bytes, " +
    23  			"so that we can test out our chunking loop." +
    24  			"So to make it that long, we'll just continue jabbering about nothing. " +
    25  			"Nothing, nothing, nothing, nothing, nothing.",
    26  	)
    27  
    28  	enc, err := p.encryptRSA(msg)
    29  	if err != nil {
    30  		t.Fatalf("encryptRSA error: %v", err)
    31  	}
    32  
    33  	reMsg, err := p.decryptRSA(enc)
    34  	if err != nil {
    35  		t.Fatalf("decryptRSA error: %v", err)
    36  	}
    37  
    38  	if !bytes.Equal(reMsg, msg) {
    39  		t.Fatalf("wrong message. %q != %q", string(reMsg), string(msg))
    40  	}
    41  }