}
type Piece struct {
- Info *Info
+ Info *InfoEx
i int
}
return
}
-func (me *Info) Piece(i int) Piece {
+func (me *InfoEx) Piece(i int) Piece {
return Piece{me, i}
}
// important to Bittorrent.
type InfoEx struct {
Info
- Hash []byte
+ Hash *InfoHash
Bytes []byte
}
)
func (this *InfoEx) UnmarshalBencode(data []byte) error {
- this.Bytes = make([]byte, 0, len(data))
- this.Bytes = append(this.Bytes, data...)
+ this.Bytes = append(make([]byte, 0, len(data)), data...)
h := sha1.New()
_, err := h.Write(this.Bytes)
if err != nil {
panic(err)
}
- this.Hash = h.Sum(nil)
+ this.Hash = new(InfoHash)
+ missinggo.CopyExact(this.Hash, h.Sum(nil))
return bencode.Unmarshal(data, &this.Info)
}
}
type InfoHash [20]byte
+
+func (me *InfoHash) Bytes() []byte {
+ return me[:]
+}
+
+func (ih *InfoHash) AsString() string {
+ return string(ih[:])
+}
+
+func (ih *InfoHash) HexString() string {
+ return fmt.Sprintf("%x", ih[:])
+}