Sign signs arbitrary data using ECDSA.
(data []byte, privkey *ecdsa.PrivateKey)
| 68 | |
| 69 | // Sign signs arbitrary data using ECDSA. |
| 70 | func 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. |
no outgoing calls