aboutsummaryrefslogtreecommitdiff
path: root/meta
diff options
context:
space:
mode:
Diffstat (limited to 'meta')
-rw-r--r--meta/info.go5
-rw-r--r--meta/torrent.go3
-rw-r--r--meta/torrent_test.go10
3 files changed, 16 insertions, 2 deletions
diff --git a/meta/info.go b/meta/info.go
index c3f26d9..a537b46 100644
--- a/meta/info.go
+++ b/meta/info.go
@@ -19,6 +19,11 @@ type Info struct {
Pieces []byte `bencode:"pieces"` // compact mode
Private bool `bencode:"private"` // BEP-0027
RootHash []byte `bencode:"root hash"` // BEP-0030
+ Raw []byte `bencode:"-"`
+}
+
+func (i Info) Hash() [sha1.Size]byte {
+ return sha1.Sum(i.Raw)
}
// NPieces returns number of all pieces
diff --git a/meta/torrent.go b/meta/torrent.go
index 80c987b..41323a8 100644
--- a/meta/torrent.go
+++ b/meta/torrent.go
@@ -21,7 +21,6 @@ type Torrent struct {
Encoding string `bencode:"encoding,optional"`
HTTPSeeds []string `bencode:"httpseeds,optional"` // BEP-0017
Info Info `bencode:"info"`
- InfoHash []byte `bencode:"-"`
//Nodes Nodes `bencode:"nodes"` // BEP-0005
//URLList []string `bencode:"url-list,optional"`
}
@@ -31,7 +30,7 @@ func (t Torrent) String() string {
s += fmt.Sprintf("comment: %s\n", t.Comment)
s += fmt.Sprintf("date: %s\n", t.CreationDate)
s += fmt.Sprintf("%v", t.Info)
- s += fmt.Sprintf("info hash: % x", t.InfoHash)
+ s += fmt.Sprintf("info hash: % x", t.Info.Hash())
return s
}
diff --git a/meta/torrent_test.go b/meta/torrent_test.go
index 3dd8595..d8706e0 100644
--- a/meta/torrent_test.go
+++ b/meta/torrent_test.go
@@ -9,6 +9,16 @@ var tors = map[string]string{
"mul": "multi.torrent",
}
+func TestHash(t *testing.T) {
+ tor, err := New("../examples/" + tors["bsd"])
+ if err != nil {
+ t.Error(err)
+ }
+ if tor.Info.Hash() != [20]byte{0xe8, 0x40, 0x03, 0x8d, 0xea, 0x19, 0x98, 0xc3, 0x96, 0x14, 0xdc, 0xd2, 0x85, 0x94, 0x50, 0x1d, 0xf0, 0x2b, 0xd3, 0x2d} {
+ t.Errorf("wrong info hash %x", tor.Info.Hash())
+ }
+}
+
func TestPieces(t *testing.T) {
tor, err := New("../examples/" + tors["mul"])
if err != nil {