Golang – How to Sign Pieces of Data with Go-Ethereum

go-ethereumgolangsignature

I'm trying to figure out how to sign a piece of data (in this case a string) using golang. So far everything I can find is about signing transactions which wont work for signing pieces of data that is not of a type transaction.

Best Answer

Figured it out:

// convert string key to byte array
byteKey := []byte(`{....}`)

privKey, err := keystore.DecryptKey(byteKey, string(pass))
if err != nil {
    log.Fatal("error decrypting key")
} else {
    fmt.Printf("key successfully decrypted %v\n", privKey)
}

hash := crypto.Keccak256([]byte("hello"))

sig, err := crypto.Sign(hash, privKey.PrivateKey)
if err != nil {
    log.Fatal("error signing data")
} else {
    fmt.Println("signature successful ", sig)
}