Add metainfo.Hash.FromHexString
This commit is contained in:
parent
5882a3b32e
commit
7044161faa
|
@ -1,6 +1,9 @@
|
|||
package metainfo
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// 20-byte SHA1 hash used for info and pieces.
|
||||
type Hash [20]byte
|
||||
|
@ -16,3 +19,18 @@ func (h *Hash) AsString() string {
|
|||
func (h Hash) HexString() string {
|
||||
return fmt.Sprintf("%x", h[:])
|
||||
}
|
||||
|
||||
func (h *Hash) FromHexString(s string) (err error) {
|
||||
if len(s) != 40 {
|
||||
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 != 20 {
|
||||
panic(n)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue