1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Make hash a [20]byte

This commit is contained in:
Jesse Li
2019-12-22 15:19:46 -05:00
parent a786dcaf37
commit 5ebabfabcd
2 changed files with 13 additions and 10 deletions

View File

@@ -13,10 +13,13 @@ import (
// Port to listen on
const Port uint16 = 6881
// A PeerID is a 20 byte unique identifier presented to trackers and peers
type PeerID [20]byte
// Torrent encodes the metadata from a .torrent file
type Torrent struct {
Announce string
InfoHash []byte
InfoHash [20]byte
PieceHashes [][]byte
PieceLength int
Length int
@@ -37,8 +40,8 @@ type bencodeTorrent struct {
// Download downloads a torrent
func (t *Torrent) Download() error {
peerID := make([]byte, 20)
_, err := rand.Read(peerID)
peerID := PeerID{}
_, err := rand.Read(peerID[:])
if err != nil {
return err
}
@@ -62,14 +65,14 @@ func Open(r io.Reader) (*Torrent, error) {
return t, nil
}
func (i *bencodeInfo) hash() ([]byte, error) {
func (i *bencodeInfo) hash() ([20]byte, error) {
var buf bytes.Buffer
err := bencode.Marshal(&buf, *i)
if err != nil {
return nil, err
return [20]byte{}, err
}
h := sha1.Sum(buf.Bytes())
return h[:], nil
return h, nil
}
func (i *bencodeInfo) splitPieceHashes() ([][]byte, error) {