Standard library / crypto/ecdsa
crypto/ecdsa.PrivateKey.Sign()
Implements crypto.Signer and returns ASN.1 signature of the digest; opts is ignored in Go 1.23 but should describe the hash used. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| rand | io.Reader | Cryptographically secure random source; see description for exceptions. |
| digest | []byte | Digest of the message. |
| opts | crypto.SignerOpts | Algorithm-specific options; see description for nil behavior. |
Return Value
([]byte, error)
Result as described above; check any returned error before use.
Working Example
Example Snippetgo
package main
import ( "crypto" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/sha256" "fmt")
func main() { k := must(ecdsa.GenerateKey(elliptic.P256(), rand.Reader)) h := sha256.Sum256([]byte("Go")) s := must(k.Sign(rand.Reader, h[:], crypto.SHA256)) fmt.Println(ecdsa.VerifyASN1(&k.PublicKey, h[:], s))}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) }}