github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/reality/reality_test.go (about) 1 package reality 2 3 import ( 4 "crypto/rand" 5 "encoding/base64" 6 "encoding/hex" 7 "errors" 8 "fmt" 9 "testing" 10 11 "github.com/Asutorufa/yuhaiin/pkg/utils/assert" 12 "golang.org/x/crypto/curve25519" 13 ) 14 15 func TestHexDecode(t *testing.T) { 16 17 var input_base64 string 18 19 var err error 20 var privateKey []byte 21 var publicKey []byte 22 if len(input_base64) > 0 { 23 privateKey, err = base64.RawURLEncoding.DecodeString(input_base64) 24 if err != nil { 25 assert.NoError(t, err) 26 } 27 if len(privateKey) != curve25519.ScalarSize { 28 assert.NoError(t, errors.New("Invalid length of private key.")) 29 } 30 } 31 32 if privateKey == nil { 33 privateKey = make([]byte, curve25519.ScalarSize) 34 if _, err = rand.Read(privateKey); err != nil { 35 assert.NoError(t, err) 36 } 37 } 38 39 // Modify random bytes using algorithm described at: 40 // https://cr.yp.to/ecdh.html. 41 privateKey[0] &= 248 42 privateKey[31] &= 127 43 privateKey[31] |= 64 44 45 if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil { 46 assert.NoError(t, err) 47 } 48 49 fmt.Printf("Private key: %v\nPublic key: %v", 50 base64.RawURLEncoding.EncodeToString(privateKey), 51 base64.RawURLEncoding.EncodeToString(publicKey)) 52 53 t.Log(hex.DecodeString("")) 54 }