GeneratePiecesFromMemory

This commit is contained in:
whr819987540 2023-05-04 16:38:41 +08:00
parent 11405bf65b
commit 596be6e62b
1 changed files with 20 additions and 0 deletions

View File

@ -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
}
}
}