11 nextBlock: sha256.New(),
19 // How many bytes have been written to nextBlock so far.
23 func (h *Hash) remaining() int {
24 return BlockSize - h.nextBlockWritten
27 func (h *Hash) Write(p []byte) (n int, err error) {
30 n1, err = h.nextBlock.Write(p[:min(len(p), h.remaining())])
32 h.nextBlockWritten += n1
34 if h.remaining() == 0 {
35 h.blocks = append(h.blocks, h.nextBlockSum())
37 h.nextBlockWritten = 0
46 func (h *Hash) nextBlockSum() (sum [32]byte) {
47 if unsafe.SliceData(h.nextBlock.Sum(sum[:0])) != unsafe.SliceData(sum[:]) {
53 func (h *Hash) curBlocks() [][32]byte {
55 if h.nextBlockWritten != 0 {
56 blocks = append(blocks, h.nextBlockSum())
61 func (h *Hash) Sum(b []byte) []byte {
62 sum := RootWithPadHash(h.curBlocks(), [32]byte{})
63 return append(b, sum[:]...)
66 // Sums by extending with zero hashes for blocks missing to meet the given length. Necessary for
67 // piece layers hashes for file tail blocks that don't pad to the piece length.
68 func (h *Hash) SumMinLength(b []byte, length int) []byte {
69 blocks := h.curBlocks()
70 minBlocks := (length + BlockSize - 1) / BlockSize
71 blocks = append(blocks, make([][32]byte, minBlocks-len(blocks))...)
72 sum := RootWithPadHash(blocks, [32]byte{})
73 return append(b, sum[:]...)
76 func (h *Hash) Reset() {
77 h.blocks = h.blocks[:0]
79 h.nextBlockWritten = 0
82 func (h *Hash) Size() int {
86 func (h *Hash) BlockSize() int {
87 return h.nextBlock.BlockSize()
90 var _ hash.Hash = (*Hash)(nil)