optimize code and add test for x25519 command

This commit is contained in:
yuzuki999 2023-06-02 08:36:06 +08:00
parent 5379a5a668
commit 138540cd16
2 changed files with 14 additions and 8 deletions

View File

@ -23,13 +23,15 @@ func init() {
func executeX25519() { func executeX25519() {
var output string var output string
var err error var err error
defer func() {
fmt.Println(output)
}()
var privateKey []byte var privateKey []byte
var publicKey []byte var publicKey []byte
privateKey = make([]byte, curve25519.ScalarSize) privateKey = make([]byte, curve25519.ScalarSize)
if _, err = rand.Read(privateKey); err != nil { if _, err = rand.Read(privateKey); err != nil {
output = err.Error() output = fmt.Sprintf("read rand error: %s", err)
goto out return
} }
// Modify random bytes using algorithm described at: // Modify random bytes using algorithm described at:
@ -39,14 +41,11 @@ func executeX25519() {
privateKey[31] |= 64 privateKey[31] |= 64
if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil { if publicKey, err = curve25519.X25519(privateKey, curve25519.Basepoint); err != nil {
output = err.Error() output = fmt.Sprintf("gen X25519 error: %s", err)
goto out return
} }
output = fmt.Sprintf("Private key: %v\nPublic key: %v", output = fmt.Sprintf("Private key: %v\nPublic key: %v",
base64.RawURLEncoding.EncodeToString(privateKey), base64.RawURLEncoding.EncodeToString(privateKey),
base64.RawURLEncoding.EncodeToString(publicKey)) base64.RawURLEncoding.EncodeToString(publicKey))
out:
fmt.Println(output)
} }

7
cmd/x25519_test.go Normal file
View File

@ -0,0 +1,7 @@
package cmd
import "testing"
func Test_executeX25519(t *testing.T) {
executeX25519()
}