libnmdc/tth.go

41 lines
970 B
Go

package libnmdc
import (
"encoding/base32"
"errors"
"github.com/cxmcc/tiger"
)
func tth(input []byte) (string, error) {
if len(input) > 1024 {
return "", errors.New("TTH content exceeded 1024 bytes")
}
leafHash := tiger.New()
//buff := make([]byte, 1024)
//leafHash.Write([]byte("\x00"))
//copy(buff[1:], []byte(input))
//leafHash.Write(buff)
//fmt.Printf("%#v\n", buff)
leafHash.Write([]byte("\x00" + string(input)))
leafHashBytes := leafHash.Sum(nil)
rootHash := tiger.New()
//rootHash.Write([]byte("\x01"))
//rootHash.Write(leafHashBytes)
rootHash.Write([]byte("\x01" + string(leafHashBytes)))
rootHashBytes := rootHash.Sum(nil)
/*
* The Tiger Hash function, modified to prepend the specified byte to the input
* data. This is useful for the THEX algorithm which prepends a 0x00 to leaf
* nodes and 0x01 to the hash nodes.
**/
// Definitely StdEncoding
return base32.StdEncoding.EncodeToString(rootHashBytes), nil
}