Skip to content

Instantly share code, notes, and snippets.

@layou233
Created December 22, 2022 09:19
Show Gist options
  • Select an option

  • Save layou233/8e9bbae62058b34e9669c66ee654dad0 to your computer and use it in GitHub Desktop.

Select an option

Save layou233/8e9bbae62058b34e9669c66ee654dad0 to your computer and use it in GitHub Desktop.
X25519 example in Go
// X25519 example in Go
// Author: layou233
// License: WTFPL
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"golang.org/x/crypto/curve25519"
)
func main() {
var (
alicePri, bobPri [32]byte
alicePub, bobPub [32]byte
)
rand.Read(alicePri[:])
rand.Read(bobPri[:])
curve25519.ScalarBaseMult(&alicePub, &alicePri)
curve25519.ScalarBaseMult(&bobPub, &bobPri)
aliceSecret, err := curve25519.X25519(alicePri[:], bobPub[:])
if err != nil {
panic(err)
}
bobSecret, err := curve25519.X25519(bobPri[:], alicePub[:])
if err != nil {
panic(err)
}
println(len(aliceSecret))
fmt.Println(hex.EncodeToString(aliceSecret))
fmt.Println(hex.EncodeToString(bobSecret))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment