mirror of
				https://codeberg.org/cunicu/cunicu.git
				synced 2025-10-31 23:42:34 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			45 lines
		
	
	
		
			743 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			743 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package crypto_test
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"testing"
 | |
| 
 | |
| 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
 | |
| 	"riasc.eu/wice/internal/util"
 | |
| 	"riasc.eu/wice/pkg/crypto"
 | |
| )
 | |
| 
 | |
| func TestCurve25519Crypt(t *testing.T) {
 | |
| 	keyA, err := wgtypes.GeneratePrivateKey()
 | |
| 	if err != nil {
 | |
| 		t.Fail()
 | |
| 	}
 | |
| 
 | |
| 	keyB, err := wgtypes.GeneratePrivateKey()
 | |
| 	if err != nil {
 | |
| 		t.Fail()
 | |
| 	}
 | |
| 
 | |
| 	pubA := keyA.PublicKey()
 | |
| 	pubB := keyB.PublicKey()
 | |
| 
 | |
| 	payload, err := util.GenerateRandomBytes(32)
 | |
| 	if err != nil {
 | |
| 		t.Fail()
 | |
| 	}
 | |
| 
 | |
| 	encPayload, err := crypto.Curve25519Crypt(keyA[:], pubB[:], payload)
 | |
| 	if err != nil {
 | |
| 		t.Fail()
 | |
| 	}
 | |
| 
 | |
| 	decPayload, err := crypto.Curve25519Crypt(keyB[:], pubA[:], encPayload)
 | |
| 	if err != nil {
 | |
| 		t.Fail()
 | |
| 	}
 | |
| 
 | |
| 	if !bytes.Equal(decPayload, payload) {
 | |
| 		t.Fail()
 | |
| 	}
 | |
| }
 | 
