12 // 20-byte SHA1 hash used for info and pieces.
15 var _ fmt.Formatter = (*T)(nil)
17 func (t T) Format(f fmt.State, c rune) {
18 // TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
19 // with the "default" 'v', or .String() already returning the hex.
20 f.Write([]byte(t.HexString()))
23 func (t T) Bytes() []byte {
27 func (t T) AsString() string {
31 func (t T) String() string {
35 func (t T) HexString() string {
36 return fmt.Sprintf("%x", t[:])
39 func (t *T) FromHexString(s string) (err error) {
41 err = fmt.Errorf("hash hex string has bad length: %d", len(s))
44 n, err := hex.Decode(t[:], []byte(s))
55 _ encoding.TextUnmarshaler = (*T)(nil)
56 _ encoding.TextMarshaler = T{}
59 func (t *T) UnmarshalText(b []byte) error {
60 return t.FromHexString(string(b))
63 func (t T) MarshalText() (text []byte, err error) {
64 return []byte(t.HexString()), nil
67 func FromHexString(s string) (h T) {
68 err := h.FromHexString(s)
75 func HashBytes(b []byte) (ret T) {
78 copy(ret[:], hasher.Sum(nil))