Eckelj/removed hashing twice and decoding issue (#55)

removed not working hex.DecodeString(cid)

removed potential double hashing (hashing the hash with the same hash method)
removed the double hashing and the encoding issue from the serverside asset message verification code.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2023-07-31 08:12:30 +02:00 committed by GitHub
parent 229b649a69
commit 34182773dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 11 deletions

View File

@ -1,7 +1,6 @@
package sample package sample
import ( import (
"crypto/sha256"
"encoding/hex" "encoding/hex"
machinetypes "planetmint-go/x/machine/types" machinetypes "planetmint-go/x/machine/types"
@ -85,10 +84,8 @@ func Asset(sk string) (string, string) {
skBytes, _ := hex.DecodeString(sk) skBytes, _ := hex.DecodeString(sk)
privKey := &secp256k1.PrivKey{Key: skBytes} privKey := &secp256k1.PrivKey{Key: skBytes}
cidBytes, _ := hex.DecodeString(cid) cid_bytes := []byte(cid)
hash := sha256.Sum256(cidBytes) sign, _ := privKey.Sign(cid_bytes)
sign, _ := privKey.Sign(hash[:])
signatureHex := hex.EncodeToString(sign) signatureHex := hex.EncodeToString(sign)

View File

@ -2,7 +2,6 @@ package keeper
import ( import (
"context" "context"
"crypto/sha256"
"encoding/hex" "encoding/hex"
"errors" "errors"
@ -39,18 +38,15 @@ func (k msgServer) NotarizeAsset(goCtx context.Context, msg *types.MsgNotarizeAs
func ValidateSignature(message string, signature string, publicKey string) bool { func ValidateSignature(message string, signature string, publicKey string) bool {
// Convert the message, signature, and public key from hex to bytes // Convert the message, signature, and public key from hex to bytes
messageBytes, _ := hex.DecodeString(message) messageBytes := []byte(message)
signatureBytes, _ := hex.DecodeString(signature) signatureBytes, _ := hex.DecodeString(signature)
publicKeyBytes, _ := hex.DecodeString(publicKey) publicKeyBytes, _ := hex.DecodeString(publicKey)
// Hash the message
hash := sha256.Sum256(messageBytes)
// Create a secp256k1 public key object // Create a secp256k1 public key object
pubKey := &secp256k1.PubKey{Key: publicKeyBytes} pubKey := &secp256k1.PubKey{Key: publicKeyBytes}
// Verify the signature // Verify the signature
isValid := pubKey.VerifySignature(hash[:], signatureBytes) isValid := pubKey.VerifySignature(messageBytes, signatureBytes)
return isValid return isValid
} }