diff options
Diffstat (limited to 'cmd/mybittorrent/meta.go')
| -rw-r--r-- | cmd/mybittorrent/meta.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/cmd/mybittorrent/meta.go b/cmd/mybittorrent/meta.go new file mode 100644 index 0000000..34369c4 --- /dev/null +++ b/cmd/mybittorrent/meta.go @@ -0,0 +1,37 @@ +package main + +import ( + "crypto/sha1" + + bencode "github.com/jackpal/bencode-go" +) + +type Meta struct { + Announce string `bencode:"announce"` + Info FileInfo `bencode:"info"` +} + +type FileInfo struct { + Length int `bencode:"length"` + Name string `bencode:"name"` + PieceLength int `bencode:"piece length"` + Pieces string `bencode:"pieces"` +} + +func (m Meta) InfoHash() ([]byte, error) { + sha := sha1.New() + if err := bencode.Marshal(sha, m.Info); err != nil { + return nil, err + } + + return sha.Sum(nil), nil +} + +func (m Meta) PieceHashes() []string { + hashes := make([]string, 0, len(m.Info.Pieces)/20) + for i := 0; i < len(m.Info.Pieces); i += 20 { + hashes = append(hashes, m.Info.Pieces[i:i+20]) + } + + return hashes +} |