crypto/ecdh

Guided tour · Crypto · pkg.go.dev →

Elliptic-curve Diffie-Hellman key exchange. The modern, misuse-resistant replacement for crypto/elliptic's low-level API.

Key agreement

Two parties derive the same shared secret

curve := ecdh.X25519()
aliceKey, _ := curve.GenerateKey(rand.Reader)
bobKey, _ := curve.GenerateKey(rand.Reader)

aliceShared, _ := aliceKey.ECDH(bobKey.PublicKey())
bobShared, _   := bobKey.ECDH(aliceKey.PublicKey())
// aliceShared == bobShared — feed to HKDF to derive session keys