mirror of
https://github.com/postmannen/ctrl.git
synced 2025-03-15 10:57:42 +00:00
initial implementation of hash check for copy messages
This commit is contained in:
parent
69e0bd3696
commit
2745374da3
1 changed files with 15 additions and 1 deletions
|
@ -2,6 +2,7 @@ package steward
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
@ -311,6 +312,7 @@ type copySubData struct {
|
||||||
CopyStatus copyStatus
|
CopyStatus copyStatus
|
||||||
CopyData []byte
|
CopyData []byte
|
||||||
ChunkNumber int
|
ChunkNumber int
|
||||||
|
Hash [32]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context, chan Message) error {
|
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.
|
// We set the default status to copyData. If we get an io.EOF we change it to copyDone later.
|
||||||
status := copyData
|
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)
|
b := make([]byte, cia.SplitChunkSize)
|
||||||
_, err := fh.Read(b)
|
_, err := fh.Read(b)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
|
@ -358,6 +360,9 @@ func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context,
|
||||||
status = copyDone
|
status = copyDone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a hash of the bytes
|
||||||
|
hash := sha256.Sum256(b)
|
||||||
|
|
||||||
chunkNumber++
|
chunkNumber++
|
||||||
|
|
||||||
// Create message and send data to dst
|
// Create message and send data to dst
|
||||||
|
@ -367,6 +372,7 @@ func copySrcSubProcFunc(proc process, cia copyInitialData) func(context.Context,
|
||||||
CopyStatus: status,
|
CopyStatus: status,
|
||||||
CopyData: b,
|
CopyData: b,
|
||||||
ChunkNumber: chunkNumber,
|
ChunkNumber: chunkNumber,
|
||||||
|
Hash: hash,
|
||||||
}
|
}
|
||||||
|
|
||||||
csaSerialized, err := cbor.Marshal(csa)
|
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)
|
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 {
|
switch csa.CopyStatus {
|
||||||
case copyData:
|
case copyData:
|
||||||
// Write the data chunk to disk ?
|
// Write the data chunk to disk ?
|
||||||
|
|
Loading…
Add table
Reference in a new issue