From 4bed2bc49e310371431ca5f4b55729a6a246a36e Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 27 Jun 2016 03:27:45 +0200 Subject: Slice checksum --- meta/torrent.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'meta/torrent.go') diff --git a/meta/torrent.go b/meta/torrent.go index b5a4bb2..f5e64b9 100644 --- a/meta/torrent.go +++ b/meta/torrent.go @@ -26,6 +26,23 @@ type Info struct { Private bool `bencode:"private"` } +func (i Info) NPieces() int { + return len(i.Pieces) / 20 +} + +func (i Info) CheckSum(n int) []byte { + off := n * 20 + return i.Pieces[off : off+20] +} + +func (i Info) CheckSums() [][]byte { + cs := make([][]byte, i.NPieces()) + for n := 0; n < i.NPieces(); n++ { + cs[n] = i.CheckSum(n) + } + return cs +} + func (i Info) FullPath(n int) (string, error) { if i.isSingleFile() { return i.Name, nil -- cgit v1.2.3