Standard library / crypto/ed25519

crypto/ed25519.PrivateKey.Sign()

Implements crypto.Signer; random source ignored. Hash 0 accepts raw message, SHA512 selects Ed25519ph and requires 64-byte prehash. Nonempty context selects context mode or binds the prehash variant. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error)

Parameters & Attributes

NameTypeDescription
randio.ReaderCryptographically secure random source; see description for exceptions.
message[]byteMessage bytes or prehash as specified.
optscrypto.SignerOptsAlgorithm-specific options; see description for nil behavior.

Return Value

(signature []byte, err error)

Result as described above; check any returned error before use.

Working Example

Example Snippetgo
package main
import (  "crypto"  "crypto/ed25519"  "crypto/rand"  "crypto/sha512"  "fmt")
func main() {  pub, k, err := ed25519.GenerateKey(rand.Reader)  check(err)  h := sha512.Sum512([]byte("Go"))  o := &ed25519.Options{Hash: crypto.SHA512}  s := must(k.Sign(nil, h[:], o))  fmt.Println(ed25519.VerifyWithOptions(pub, h[:], s, o) == nil)}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference