MCPcopy
hub / github.com/gtank/cryptopasta / Sign

Function Sign

sign.go:70–90  ·  view source on GitHub ↗

Sign signs arbitrary data using ECDSA.

(data []byte, privkey *ecdsa.PrivateKey)

Source from the content-addressed store, hash-verified

68
69// Sign signs arbitrary data using ECDSA.
70func Sign(data []byte, privkey *ecdsa.PrivateKey) ([]byte, error) {
71 // hash message
72 digest := sha256.Sum256(data)
73
74 // sign the hash
75 r, s, err := ecdsa.Sign(rand.Reader, privkey, digest[:])
76 if err != nil {
77 return nil, err
78 }
79
80 // encode the signature {R, S}
81 // big.Int.Bytes() will need padding in the case of leading zero bytes
82 params := privkey.Curve.Params()
83 curveOrderByteSize := params.P.BitLen() / 8
84 rBytes, sBytes := r.Bytes(), s.Bytes()
85 signature := make([]byte, curveOrderByteSize*2)
86 copy(signature[curveOrderByteSize-len(rBytes):], rBytes)
87 copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes)
88
89 return signature, nil
90}
91
92// Verify checks a raw ECDSA signature.
93// Returns true if it's valid and false if not.

Callers 2

TestSignFunction · 0.85
TestSignWithP384Function · 0.85

Calls

no outgoing calls

Tested by 2

TestSignFunction · 0.68
TestSignWithP384Function · 0.68