]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Introduce metainfo.HashSize
authorMatt Joiner <anacrolix@gmail.com>
Fri, 29 Dec 2017 01:17:58 +0000 (12:17 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 29 Dec 2017 01:17:58 +0000 (12:17 +1100)
This will be useful when BitTorrent 2 is implemented, and helps with creating arbitrary Info.Pieces

metainfo/hash.go
metainfo/piece.go

index d1c832b0bdd50d8b97446ca3b837b1c6428fef31..511647ed730b680ffe58c25ea48db38849fbf3cb 100644 (file)
@@ -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
index ff753bd3c88d0f83d475fa91e5c354be1f06ae10..55cdc43c143e32d7e1b183e0250e00da076ac437 100644 (file)
@@ -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
 }