enhancement/signature-verification (#6)

This commit is contained in:
Dean Eigenmann 2019-08-05 17:19:01 +02:00 committed by GitHub
parent 17145c3dd5
commit 93625382b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 8 deletions

View File

@ -3,8 +3,6 @@ package client
import (
"crypto/ecdsa"
"crypto/sha256"
"encoding/binary"
"log"
"github.com/ethereum/go-ethereum/crypto"
@ -99,6 +97,15 @@ func (c *Client) onReceive(message mvdsproto.Message) {
return
}
pubkey, err := crypto.SigToPub(msg.ID(), msg.Signature)
if err != nil {
log.Printf("error while recovering pubkey: %s", err.Error())
// @todo
return
}
// @todo probably store the sender somewhere?
// @todo pump messages to subscriber channels
if len(msg.PreviousMessage) == 0 {
@ -124,12 +131,7 @@ func (c *Client) handlePreviousMessage(group state.GroupID, previousMessage stat
// sign signs generates a signature of the message and adds it to the message.
func (c *Client) sign(m *protobuf.Message) error {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(m.MessageType))
b = append(b, m.Body...)
b = append(b, m.PreviousMessage...)
hash := sha256.Sum256(b)
hash := m.ID()
sig, err := crypto.Sign(hash[:], c.identity)
if err != nil {

16
protobuf/messageid.go Normal file
View File

@ -0,0 +1,16 @@
package protobuf
import (
"crypto/sha256"
"encoding/binary"
)
func (m *Message) ID() []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(m.MessageType))
b = append(b, m.Body...)
b = append(b, m.PreviousMessage...)
hash := sha256.Sum256(b)
return hash[:]
}