metainfo/hash.go | 8 +++++--- metainfo/piece.go | 2 +- diff --git a/metainfo/hash.go b/metainfo/hash.go index d1c832b0bdd50d8b97446ca3b837b1c6428fef31..511647ed730b680ffe58c25ea48db38849fbf3cb 100644 --- a/metainfo/hash.go +++ b/metainfo/hash.go @@ -6,8 +6,10 @@ "encoding/hex" "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 @@ return fmt.Sprintf("%x", h[:]) } 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 @@ n, err := hex.Decode(h[:], []byte(s)) if err != nil { return } - if n != 20 { + if n != HashSize { panic(n) } return diff --git a/metainfo/piece.go b/metainfo/piece.go index ff753bd3c88d0f83d475fa91e5c354be1f06ae10..55cdc43c143e32d7e1b183e0250e00da076ac437 100644 --- a/metainfo/piece.go +++ b/metainfo/piece.go @@ -19,7 +19,7 @@ return int64(p.i) * p.Info.PieceLength } 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 }