-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
80 lines (65 loc) · 1.42 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package metainfo
import (
"crypto/sha1"
"encoding"
"encoding/hex"
"fmt"
)
const HashSize = 20
// 20-byte SHA1 hash used for info and pieces.
type Hash [HashSize]byte
var _ fmt.Formatter = (*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
// with the "default" 'v', or .String() already returning the hex.
f.Write([]byte(h.HexString()))
}
func (h Hash) Bytes() []byte {
return h[:]
}
func (h Hash) AsString() string {
return string(h[:])
}
func (h Hash) String() string {
return h.HexString()
}
func (h Hash) HexString() string {
return fmt.Sprintf("%x", h[:])
}
func (h *Hash) FromHexString(s string) (err error) {
if len(s) != 2*HashSize {
err = fmt.Errorf("hash hex string has bad length: %d", len(s))
return
}
n, err := hex.Decode(h[:], []byte(s))
if err != nil {
return
}
if n != HashSize {
panic(n)
}
return
}
var (
_ encoding.TextUnmarshaler = (*Hash)(nil)
_ encoding.TextMarshaler = Hash{}
)
func (h *Hash) UnmarshalText(b []byte) error {
return h.FromHexString(string(b))
}
func (h Hash) MarshalText() (text []byte, err error) {
return []byte(h.HexString()), nil
}
func NewHashFromHex(s string) (h Hash) {
err := h.FromHexString(s)
if err != nil {
panic(err)
}
return
}
func HashBytes(b []byte) (ret Hash) {
hasher := sha1.New()
hasher.Write(b)
copy(ret[:], hasher.Sum(nil))
return
}