diff options
author | axtloss <axtlos@getcryst.al> | 2024-02-03 17:34:17 +0100 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-02-03 17:34:17 +0100 |
commit | 09f7f5fe7b55a6ab2e2326aa7ff27cf7f7bc05ba (patch) | |
tree | 12577c54b02e60c5ce5850ef2a2e65982d7c6849 /core | |
parent | abd7aba9709a7e05d57861af50f55ce275fb3663 (diff) | |
download | fsverify-09f7f5fe7b55a6ab2e2326aa7ff27cf7f7bc05ba.tar.gz fsverify-09f7f5fe7b55a6ab2e2326aa7ff27cf7f7bc05ba.tar.bz2 |
fix typos
Diffstat (limited to 'core')
-rw-r--r-- | core/verification.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/core/verification.go b/core/verification.go new file mode 100644 index 0000000..5023d06 --- /dev/null +++ b/core/verification.go @@ -0,0 +1,41 @@ +package core + +import ( + "bufio" + "fmt" + "strings" +) + +func ReadBlock(node Node, part *bufio.Reader) ([]byte, error) { + block := make([]byte, node.BlockEnd-node.BlockStart) + blockSize := node.BlockEnd - node.BlockStart + _, err := part.Discard(node.BlockStart) + if err != nil { + return []byte{}, err + } + block, err = part.Peek(blockSize) + return block, err +} + +func VerifyBlock(block []byte, node Node) error { + calculatedBlockHash, err := CalculateBlockHash(block) + if err != nil { + return err + } + wantedBlockHash := node.BlockSum + if strings.Compare(calculatedBlockHash, strings.TrimSpace(wantedBlockHash)) == 0 { + return nil + } + return fmt.Errorf("Error: Node %s ranging from %d to %d does not match block", node.PrevNodeSum, node.BlockStart, node.BlockEnd) +} + +func VerifyNode(node Node, nextNode Node) error { + nodeHash, err := calculateStringHash(fmt.Sprintf("%d%d%s%s", node.BlockStart, node.BlockEnd, node.BlockSum, node.PrevNodeSum)) + if err != nil { + return err + } + if strings.Compare(nodeHash, nextNode.PrevNodeSum) != 0 { + return fmt.Errorf("Node %s is not valid!", node.PrevNodeSum) + } + return nil +} |