Standard library / crypto/ecdsa
crypto/ecdsa.SignASN1()
Produces an ASN.1 DER encoded ECDSA signature for a precomputed digest. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func SignASN1(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, 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
([]byte, 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")) s := must(ecdsa.SignASN1(rand.Reader, k, h[:])) 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) }}