github.com/incognitochain/go-incognito-sdk@v1.0.1/privacy/hybridencryption_test.go (about) 1 package privacy 2 3 import ( 4 "crypto/rand" 5 "github.com/stretchr/testify/assert" 6 "testing" 7 ) 8 9 /* 10 Unit test for Hybrid encryption 11 */ 12 func TestHybridEncryption(t *testing.T) { 13 for i := 0; i < 5000; i++ { 14 // random message 15 msg := randomMessage() 16 17 // generate key pair for ElGamal 18 privKey := new(elGamalPrivateKey) 19 privKey.x = RandomScalar() 20 21 // generate public key 22 pubKey := new(elGamalPublicKey) 23 pubKey.h = new(Point).ScalarMultBase(privKey.x) 24 25 // encrypt message using public key 26 ciphertext, err := HybridEncrypt(msg, pubKey.h) 27 28 assert.Equal(t, nil, err) 29 30 // convert HybridCipherText to bytes array 31 ciphertextBytes := ciphertext.Bytes() 32 33 // new HybridCipherText to set bytes array 34 ciphertext2 := new(HybridCipherText) 35 err2 := ciphertext2.SetBytes(ciphertextBytes) 36 37 assert.Equal(t, nil, err2) 38 assert.Equal(t, ciphertext, ciphertext2) 39 40 // decrypt message using private key 41 msg2, err := HybridDecrypt(ciphertext2, privKey.x) 42 43 assert.Equal(t, nil, err) 44 assert.Equal(t, msg, msg2) 45 } 46 } 47 48 func randomMessage() []byte { 49 msg := make([]byte, 128) 50 rand.Read(msg) 51 return msg 52 }