neo-go/pkg/crypto/publickey/TestHelper/helper.go

34 lines
742 B
Go
Raw Normal View History

2019-02-25 22:44:14 +00:00
package pubkeytesthelper
import (
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/crypto/privatekey"
)
// SignDataWithRandomPrivateKey will sign data with
// a random private key, then verify said data
// returning true if Verify returns true
2019-02-25 22:44:14 +00:00
func SignDataWithRandomPrivateKey(data []byte) (bool, error) {
hashedData, err := hash.Sha256(data)
if err != nil {
return false, err
}
2019-02-25 22:44:14 +00:00
privKey, err := privatekey.NewPrivateKey()
if err != nil {
return false, err
}
2019-02-25 22:44:14 +00:00
signedData, err := privKey.Sign(data)
if err != nil {
return false, err
}
pubKey, err := privKey.PublicKey()
if err != nil {
return false, err
}
result := pubKey.Verify(signedData, hashedData.Bytes())
2019-02-25 22:44:14 +00:00
return result, nil
}