mirror of
https://github.com/wyx2685/V2bX.git
synced 2025-01-22 18:08:14 -05:00
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"crypto/rand"
|
||
|
"encoding/base64"
|
||
|
"fmt"
|
||
|
"github.com/spf13/cobra"
|
||
|
"golang.org/x/crypto/curve25519"
|
||
|
)
|
||
|
|
||
|
var x25519Command = cobra.Command{
|
||
|
Use: "x25519",
|
||
|
Short: "Generate key pair for x25519 key exchange",
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
executeX25519()
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
command.AddCommand(&x25519Command)
|
||
|
}
|
||
|
|
||
|
func executeX25519() {
|
||
|
var output string
|
||
|
var err error
|
||
|
var privateKey []byte
|
||
|
var publicKey []byte
|
||
|
|
||
|
privateKey = make([]byte, curve25519.ScalarSize)
|
||
|
if _, err = rand.Read(privateKey); err != nil {
|
||
|
output = err.Error()
|
||
|
goto out
|
||
|
}
|
||
|
|
||
|
// Modify random bytes using algorithm described at:
|
||
|
// https://cr.yp.to/ecdh.html.
|
||
|
privateKey[0] &= 248
|
||
|
privateKey[31] &= 127
|
||
|
privateKey[31] |= 64
|
||
|
|
||
|
if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil {
|
||
|
output = err.Error()
|
||
|
goto out
|
||
|
}
|
||
|
|
||
|
output = fmt.Sprintf("Private key: %v\nPublic key: %v",
|
||
|
base64.RawURLEncoding.EncodeToString(privateKey),
|
||
|
base64.RawURLEncoding.EncodeToString(publicKey))
|
||
|
|
||
|
out:
|
||
|
fmt.Println(output)
|
||
|
}
|