Skip to content

Commit

Permalink
check integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
baarayy committed May 16, 2024
1 parent c83bf18 commit c296134
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package p2p

import (
"bytes"
"crypto/sha1"
"fmt"
"magnetflow/client"
"magnetflow/message"
"magnetflow/peers"
Expand Down Expand Up @@ -104,3 +107,11 @@ func attemptDownloadPiece(c *client.Client, pw *pieceWork) ([]byte, error) {
}
return state.buf, nil
}

func checkIntegrity(pw *pieceWork, buf []byte) error {
hash := sha1.Sum(buf)
if !bytes.Equal(hash[:], pw.hash[:]) {
return fmt.Errorf("Piece %d failed integrity check", pw.index)
}
return nil
}

0 comments on commit c296134

Please sign in to comment.