import (
"crypto/sha1"
+ "encoding"
"encoding/hex"
"fmt"
)
// 20-byte SHA1 hash used for info and pieces.
type Hash [HashSize]byte
-var _ fmt.Formatter = (*Hash)(nil)
+var (
+ _ fmt.Formatter = (*Hash)(nil)
+ _ encoding.TextUnmarshaler = (*Hash)(nil)
+)
func (h Hash) Format(f fmt.State, c rune) {
// TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
return
}
+func (h *Hash) UnmarshalText(b []byte) error {
+ return h.FromHexString(string(b))
+}
+
func NewHashFromHex(s string) (h Hash) {
err := h.FromHexString(s)
if err != nil {