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