GeneratePiecesFromMemory
This commit is contained in:
parent
11405bf65b
commit
596be6e62b
|
@ -1,6 +1,7 @@
|
|||
package metainfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha1"
|
||||
"io"
|
||||
)
|
||||
|
@ -20,3 +21,22 @@ func GeneratePieces(r io.Reader, pieceLength int64, b []byte) ([]byte, error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GeneratePiecesFromMemory(data []byte, pieceLength int64, len int, b []byte) ([]byte, error) {
|
||||
reader := io.NewSectionReader(bytes.NewReader(data), 0, int64(len))
|
||||
for {
|
||||
h := sha1.New()
|
||||
// copy pieceLength bytes from reader to h
|
||||
num, err := io.CopyN(h, reader, pieceLength)
|
||||
if num > 0 {
|
||||
b = h.Sum(b)
|
||||
}
|
||||
if err == io.EOF {
|
||||
return b, nil
|
||||
}
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue