From 5ac3e9ae5484c946afce9a664ed0d9e53e6143b7 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 29 Dec 2017 12:17:58 +1100 Subject: [PATCH] Introduce metainfo.HashSize This will be useful when BitTorrent 2 is implemented, and helps with creating arbitrary Info.Pieces --- metainfo/hash.go | 8 +++++--- metainfo/piece.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/metainfo/hash.go b/metainfo/hash.go index d1c832b0..511647ed 100644 --- a/metainfo/hash.go +++ b/metainfo/hash.go @@ -6,8 +6,10 @@ import ( "fmt" ) +const HashSize = 20 + // 20-byte SHA1 hash used for info and pieces. -type Hash [20]byte +type Hash [HashSize]byte func (h Hash) Bytes() []byte { return h[:] @@ -26,7 +28,7 @@ func (h Hash) HexString() string { } func (h *Hash) FromHexString(s string) (err error) { - if len(s) != 40 { + if len(s) != 2*HashSize { err = fmt.Errorf("hash hex string has bad length: %d", len(s)) return } @@ -34,7 +36,7 @@ func (h *Hash) FromHexString(s string) (err error) { if err != nil { return } - if n != 20 { + if n != HashSize { panic(n) } return diff --git a/metainfo/piece.go b/metainfo/piece.go index ff753bd3..55cdc43c 100644 --- a/metainfo/piece.go +++ b/metainfo/piece.go @@ -19,7 +19,7 @@ func (p Piece) Offset() int64 { } func (p Piece) Hash() (ret Hash) { - missinggo.CopyExact(&ret, p.Info.Pieces[p.i*20:(p.i+1)*20]) + missinggo.CopyExact(&ret, p.Info.Pieces[p.i*HashSize:(p.i+1)*HashSize]) return } -- 2.48.1