import ( "crypto/elliptic" "crypto/rand" "fmt" ) func main() { curve := elliptic.P256() priv, err := ecdsa.GenerateKey(curve, rand.Reader) if err != nil { fmt.Printf("Error generating key pair: %s", err.Error()) return } fmt.Printf("Private Key: %x", priv.D) fmt.Printf("Public Key: %x", append(priv.PublicKey.X.Bytes(), priv.PublicKey.Y.Bytes()...)) }
import ( "crypto/ecdsa" "crypto/rand" "crypto/sha256" "fmt" ) func main() { curve := elliptic.P256() priv, err := ecdsa.GenerateKey(curve, rand.Reader) if err != nil { fmt.Printf("Error generating key pair: %s", err.Error()) return } message := []byte("hello world") hash := sha256.Sum256(message) r, s, err := ecdsa.Sign(rand.Reader, priv, hash[:]) if err != nil { fmt.Printf("Error signing message: %s", err.Error()) return } valid := ecdsa.Verify(&priv.PublicKey, hash[:], r, s) if valid { fmt.Println("Message signature is valid!") } else { fmt.Println("Message signature is invalid!") } }In both examples, the package library used is "crypto/elliptic".