1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

initial implementation of hash check for copy messages

This commit is contained in:
postmannen 2022-06-15 10:38:53 +02:00
parent 69e0bd3696
commit 2745374da3

View file

@ -2,6 +2,7 @@ package steward
import (
"context"
"crypto/sha256"
"fmt"
"io"
"log"
@ -311,6 +312,7 @@ type copySubData struct {
CopyStatus copyStatus
CopyData []byte
ChunkNumber int
Hash [32]byte
}
func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context, chan Message) error {
@ -348,7 +350,7 @@ func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context,
// We set the default status to copyData. If we get an io.EOF we change it to copyDone later.
status := copyData
log.Printf(" * RECEIVED in copySrcSubProcFunc * copyStatus=copyReady: %v\n\n", csa.CopyStatus)
log.Printf(" * RECEIVED in copySrcSubProcFunc from dst * copyStatus=copyReady: %v\n\n", csa.CopyStatus)
b := make([]byte, cia.SplitChunkSize)
_, err := fh.Read(b)
if err != nil && err != io.EOF {
@ -358,6 +360,9 @@ func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context,
status = copyDone
}
// Create a hash of the bytes
hash := sha256.Sum256(b)
chunkNumber++
// Create message and send data to dst
@ -367,6 +372,7 @@ func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context,
CopyStatus: status,
CopyData: b,
ChunkNumber: chunkNumber,
Hash: hash,
}
csaSerialized, err := cbor.Marshal(csa)
@ -458,6 +464,14 @@ func copyDstSubProcFunc(proc process, cia copyInitialData, message Message) func
log.Fatalf("error: copySrcSubHandler: cbor unmarshal of csa failed: %v\n", err)
}
// Check if the hash matches.
hash := sha256.Sum256(csa.CopyData)
if hash != csa.Hash {
log.Fatalf("error: hash of received message is not correct\n")
}
fmt.Printf(" * DEBUG: Hash was verified OK\n")
switch csa.CopyStatus {
case copyData:
// Write the data chunk to disk ?