V2bX/cmd/x25519.go
Yuzuki616 ca67855ec9 update
fix deps cycle
update example
fix color for X25519
output supported cores message
2023-06-08 03:11:42 +08:00

54 lines
1.1 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
defer func() {
fmt.Println(output)
}()
var privateKey []byte
var publicKey []byte
privateKey = make([]byte, curve25519.ScalarSize)
if _, err = rand.Read(privateKey); err != nil {
output = Err("read rand error: ", err)
return
}
// 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("gen X25519 error: ", err)
return
}
output = fmt.Sprint("Private key: ",
base64.RawURLEncoding.EncodeToString(privateKey),
"\nPublic key: ",
base64.RawURLEncoding.EncodeToString(publicKey))
}