func (mh *merkleHash) Sum(b []byte) []byte {
	var (
		curBlock = []byte{}
		offset   int
	)
	if mh.partialLastNode {
		// if this is true, then we need to pop the last node
		mh.tree.Nodes = mh.tree.Nodes[:len(mh.tree.Nodes)-1]
		mh.partialLastNode = false
	}

	if mh.lastBlockLen > 0 {
		curBlock = append(curBlock[:], mh.lastBlock[:mh.lastBlockLen]...)
		mh.lastBlockLen = 0
	}

	if b != nil && len(b) > 0 {
		curBlock = append(curBlock, b...)
	}

	// incase we're at a new or reset state
	if len(mh.tree.Nodes) == 0 && len(curBlock) == 0 {
		return nil
	}

	for i := 0; i < len(curBlock)/mh.blockSize; i++ {
		n, err := NewNodeHashBlock(mh.hm, curBlock[offset:(offset+mh.blockSize)])
		if err != nil {
			// XXX i hate to swallow an error here, but the `Sum() []byte` signature
			// :-\
			sBuf := make([]byte, 1024)
			runtime.Stack(sBuf, false)
			fmt.Fprintf(os.Stderr, "[ERROR]: %s %q", err, string(sBuf))
			return nil
		}
		mh.tree.Nodes = append(mh.tree.Nodes, n)
		offset = offset + mh.blockSize
	}

	// If there is remainder, we'll need to make a partial node and stash it
	if m := (len(curBlock) % mh.blockSize); m != 0 {
		mh.lastBlockLen = copy(mh.lastBlock, curBlock[offset:])

		n, err := NewNodeHashBlock(mh.hm, curBlock[offset:])
		if err != nil {
			sBuf := make([]byte, 1024)
			runtime.Stack(sBuf, false)
			fmt.Fprintf(os.Stderr, "[ERROR]: %s %q", err, string(sBuf))
			return nil
		}
		mh.tree.Nodes = append(mh.tree.Nodes, n)
		mh.partialLastNode = true
	}

	sum, err := mh.tree.Root().Checksum()
	if err != nil {
		// XXX i hate to swallow an error here, but the `Sum() []byte` signature
		// :-\
		sBuf := make([]byte, 1024)
		runtime.Stack(sBuf, false)
		fmt.Fprintf(os.Stderr, "[ERROR]: %s %q", err, string(sBuf))
		return nil
	}
	return sum
}
// XXX this will be tricky, as the last block can be less than the BlockSize.
// if they get the sum, it will be mh.tree.Root().Checksum() at that point.
//
// But if they continue writing, it would mean a continuation of the bytes in
// the last block. So popping the last node, and having a buffer for the bytes
// in that last partial block.
//
// if that last block was complete, then no worries. start the next node.
XXX this will be tricky, as the last block can be less than the BlockSize.
if they get the sum, it will be mh.tree.Root().Checksum() at that point.

But if they continue writing, it would mean a continuation of the bytes in
the last block. So popping the last node, and having a buffer for the bytes
in that last partial block.

if that last block was complete, then no worries. start the next node.
// XXX this will be tricky as the last block can be less than the BlockSize . // if they get the sum it will be mh . tree . Root () . Checksum () at that point . // // But if they continue writing it would mean a continuation of the bytes in // the last block . So popping the last node and having a buffer for the bytes // in that last partial block . // // if that last block was complete then no worries . start the next node .
func (mh *merkleHash) Sum(b []byte) []byte {
	var (
		curBlock = []byte{}
		offset   int
	)
	if mh.partialLastNode {
		mh.tree.Nodes = mh.tree.Nodes[:len(mh.tree.Nodes)-1]
		mh.partialLastNode = false
	}
	if mh.lastBlockLen > 0 {
		curBlock = append(curBlock[:], mh.lastBlock[:mh.lastBlockLen]...)
		mh.lastBlockLen = 0
	}
	if b != nil && len(b) > 0 {
		curBlock = append(curBlock, b...)
	}
	if len(mh.tree.Nodes) == 0 && len(curBlock) == 0 {
		return nil
	}
	for i := 0; i < len(curBlock)/mh.blockSize; i++ {
		n, err := NewNodeHashBlock(mh.hm, curBlock[offset:(offset+mh.blockSize)])
		if err != nil {
			sBuf := make([]byte, 1024)
			runtime.Stack(sBuf, false)
			fmt.Fprintf(os.Stderr, "[ERROR]: %s %q", err, string(sBuf))
			return nil
		}
		mh.tree.Nodes = append(mh.tree.Nodes, n)
		offset = offset + mh.blockSize
	}
	if m := (len(curBlock) % mh.blockSize); m != 0 {
		mh.lastBlockLen = copy(mh.lastBlock, curBlock[offset:])
		n, err := NewNodeHashBlock(mh.hm, curBlock[offset:])
		if err != nil {
			sBuf := make([]byte, 1024)
			runtime.Stack(sBuf, false)
			fmt.Fprintf(os.Stderr, "[ERROR]: %s %q", err, string(sBuf))
			return nil
		}
		mh.tree.Nodes = append(mh.tree.Nodes, n)
		mh.partialLastNode = true
	}
	sum, err := mh.tree.Root().Checksum()
	if err != nil {
		sBuf := make([]byte, 1024)
		runtime.Stack(sBuf, false)
		fmt.Fprintf(os.Stderr, "[ERROR]: %s %q", err, string(sBuf))
		return nil
	}
	return sum
}
func ( mh * merkleHash ) Sum ( b [ ] byte ) [ ] byte { var ( curBlock = [ ] byte { } offset int ) if mh . partialLastNode { mh . tree . Nodes = mh . tree . Nodes [ : len ( mh . tree . Nodes ) - 1 ] mh . partialLastNode = false } if mh . lastBlockLen > 0 { curBlock = append ( curBlock [ : ] , mh . lastBlock [ : mh . lastBlockLen ] ... ) mh . lastBlockLen = 0 } if b != nil && len ( b ) > 0 { curBlock = append ( curBlock , b ... ) } if len ( mh . tree . Nodes ) == 0 && len ( curBlock ) == 0 { return nil } for i := 0 ; i < len ( curBlock ) / mh . blockSize ; i ++ { n , err := NewNodeHashBlock ( mh . hm , curBlock [ offset : ( offset + mh . blockSize ) ] ) if err != nil { sBuf := make ( [ ] byte , 1024 ) runtime . Stack ( sBuf , false ) fmt . Fprintf ( os . Stderr , " " , err , string ( sBuf ) ) return nil } mh . tree . Nodes = append ( mh . tree . Nodes , n ) offset = offset + mh . blockSize } if m := ( len ( curBlock ) % mh . blockSize ) ; m != 0 { mh . lastBlockLen = copy ( mh . lastBlock , curBlock [ offset : ] ) n , err := NewNodeHashBlock ( mh . hm , curBlock [ offset : ] ) if err != nil { sBuf := make ( [ ] byte , 1024 ) runtime . Stack ( sBuf , false ) fmt . Fprintf ( os . Stderr , " " , err , string ( sBuf ) ) return nil } mh . tree . Nodes = append ( mh . tree . Nodes , n ) mh . partialLastNode = true } sum , err := mh . tree . Root ( ) . Checksum ( ) if err != nil { sBuf := make ( [ ] byte , 1024 ) runtime . Stack ( sBuf , false ) fmt . Fprintf ( os . Stderr , " " , err , string ( sBuf ) ) return nil } return sum }
XXX this will be tricky, as the last block can be less than the BlockSize.
XXX this will be tricky as the last block can be less than the BlockSize .
// if this is true, then we need to pop the last node // incase we're at a new or reset state // XXX i hate to swallow an error here, but the `Sum() []byte` signature // :-\ // If there is remainder, we'll need to make a partial node and stash it // XXX i hate to swallow an error here, but the `Sum() []byte` signature // :-\