Skip to content

Commit

Permalink
Output real private key in x25519 command (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
H1JK authored Mar 8, 2023
1 parent c04c333 commit 4a0b45d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions main/commands/all/x25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"

"github.com/xtls/xray-core/main/commands/base"
"golang.org/x/crypto/curve25519"
Expand Down Expand Up @@ -44,17 +43,26 @@ func executeX25519(cmd *base.Command, args []string) {
goto out
}
}

if privateKey == nil {
privateKey = make([]byte, curve25519.ScalarSize)
if _, err = io.ReadFull(rand.Reader, privateKey); err != nil {
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))
Expand Down

0 comments on commit 4a0b45d

Please sign in to comment.