description: Tutorial on generating signatures with Go.

Generating a Signature

The components for generating a signature are: the signers private key, and the hash of the data that will be signed. Any hashing algorithm may be used as long as the output is 32 bytes. We’ll be using Keccak-256 as the hashing algorithm which is what Ethereum prefers to use.

First we’ll load private key.

  1. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  2. if err != nil {
  3. log.Fatal(err)
  4. }

Next we’ll take the Keccak-256 of the data that we wish to sign, in this case it’ll be the word hello. The go-ethereum crypto package provides a handy Keccak256Hash method for doing this.

  1. data := []byte("hello")
  2. hash := crypto.Keccak256Hash(data)
  3. fmt.Println(hash.Hex()) // 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8

Finally we sign the hash with our private, which gives us the signature.

  1. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. fmt.Println(hexutil.Encode(signature)) // 0x789a80053e4927d0a898db8e065e948f5cf086e32f9ccaa54c1908e22ac430c62621578113ddbb62d509bf6049b8fb544ab06d36f916685a2eb8e57ffadde02301

Now that we have successfully generated the signature, in the next section we’ll learn how to verify that the signature indeed was signed by the holder of that private key.

Full code

signature_generate.go

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/ethereum/go-ethereum/common/hexutil"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. )
  8. func main() {
  9. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. data := []byte("hello")
  14. hash := crypto.Keccak256Hash(data)
  15. fmt.Println(hash.Hex()) // 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
  16. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. fmt.Println(hexutil.Encode(signature)) // 0x789a80053e4927d0a898db8e065e948f5cf086e32f9ccaa54c1908e22ac430c62621578113ddbb62d509bf6049b8fb544ab06d36f916685a2eb8e57ffadde02301
  21. }