31 lines
866 B
Go
31 lines
866 B
Go
package libnmdc
|
|
|
|
import (
|
|
"encoding/base32"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/cxmcc/tiger"
|
|
)
|
|
|
|
// TTH returns the TTH hash of a string. This is a rudimentary implementation
|
|
// that only supports content up to 1024 bytes in length.
|
|
func TTH(input string) (string, error) {
|
|
|
|
// Short segments do not need to be padded.
|
|
// Content above 1024 bytes needs tree handling (0x00 prefix for leaf nodes,
|
|
// 0x01 prefix for hash nodes) but for content less than 1024 bytes, just
|
|
// return the leaf hash
|
|
// @ref http://adc.sourceforge.net/draft-jchapweske-thex-02.html
|
|
if len(input) > 1024 {
|
|
return "", errors.New("TTH content exceeded 1024 bytes")
|
|
}
|
|
|
|
// Single leaf hash only
|
|
leafHash := tiger.New()
|
|
leafHash.Write([]byte("\x00" + input))
|
|
leafHashBytes := leafHash.Sum(nil)
|
|
|
|
return strings.TrimRight(base32.StdEncoding.EncodeToString(leafHashBytes), "="), nil
|
|
}
|