github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/vmess/vmess_test.go (about)

     1  package vmess
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
     9  	"golang.org/x/crypto/chacha20poly1305"
    10  )
    11  
    12  func TestChaCHa20Poly1305(t *testing.T) {
    13  	key := make([]byte, chacha20poly1305.KeySize)
    14  	_, _ = rand.Read(key)
    15  	aead, err := chacha20poly1305.New(key)
    16  	assert.NoError(t, err)
    17  
    18  	none := make([]byte, aead.NonceSize())
    19  	_, _ = rand.Read(none)
    20  
    21  	plaintext := []byte("hello world")
    22  
    23  	cryptTxt := aead.Seal(nil, none, plaintext, nil)
    24  	t.Log(hex.EncodeToString(cryptTxt))
    25  
    26  	decryptTxt, err := aead.Open(nil, none, cryptTxt, nil)
    27  	assert.NoError(t, err)
    28  	t.Log(string(decryptTxt))
    29  }