12 // 20-byte SHA1 hash used for info and pieces.
13 type Hash [HashSize]byte
16 _ fmt.Formatter = (*Hash)(nil)
17 _ encoding.TextUnmarshaler = (*Hash)(nil)
20 func (h Hash) Format(f fmt.State, c rune) {
21 // TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
22 // with the "default" 'v', or .String() already returning the hex.
23 f.Write([]byte(h.HexString()))
26 func (h Hash) Bytes() []byte {
30 func (h Hash) AsString() string {
34 func (h Hash) String() string {
38 func (h Hash) HexString() string {
39 return fmt.Sprintf("%x", h[:])
42 func (h *Hash) FromHexString(s string) (err error) {
43 if len(s) != 2*HashSize {
44 err = fmt.Errorf("hash hex string has bad length: %d", len(s))
47 n, err := hex.Decode(h[:], []byte(s))
57 func (h *Hash) UnmarshalText(b []byte) error {
58 return h.FromHexString(string(b))
61 func NewHashFromHex(s string) (h Hash) {
62 err := h.FromHexString(s)
69 func HashBytes(b []byte) (ret Hash) {
72 copy(ret[:], hasher.Sum(nil))