Standard library / crypto/ecdsa
crypto/ecdsa.Sign()
Signs a precomputed digest and returns two signature integers r and s. Prefer SignASN1 for encoded signatures. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| rand | io.Reader | Cryptographically secure random source; see description for exceptions. |
| priv | *PrivateKey | Private key used for the operation. |
| hash | []byte | Hash algorithm, hash instance or digest as specified by the signature and description. |
Return Value
(r, s *big.Int, err error)
Result as described above; check any returned error before use.
Working Example
Example Snippetgo
package main
import ( "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/sha256" "fmt")
func main() { k := must(ecdsa.GenerateKey(elliptic.P256(), rand.Reader)) h := sha256.Sum256([]byte("Go")) r, s, err := ecdsa.Sign(rand.Reader, k, h[:]) check(err) fmt.Println(r.Sign() > 0 && s.Sign() > 0)}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) }}