1
// Copyright (c) 2016-2019 Uber Technologies, Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
22
// InfoHash is 20-byte SHA1 hash of the Info struct. It is the authoritative
23
// identifier for a torrent.
26
// NewInfoHashFromHex converts a hexidemical string into an InfoHash
27
func NewInfoHashFromHex(s string) (InfoHash, error) {
29
return InfoHash{}, fmt.Errorf("invalid hash: expected 40 characters, got %d", len(s))
32
n, err := hex.Decode(h[:], []byte(s))
34
return InfoHash{}, fmt.Errorf("invalid hex: %s", err)
37
return InfoHash{}, fmt.Errorf("invariant violation: expected 20 bytes, got %d", n)
42
// NewInfoHashFromBytes converts raw bytes to an InfoHash.
43
func NewInfoHashFromBytes(b []byte) InfoHash {
47
copy(h[:], hasher.Sum(nil))
51
// Bytes converts h to raw bytes.
52
func (h InfoHash) Bytes() []byte {
56
// Hex converts h into a hexidemical string.
57
func (h InfoHash) Hex() string {
58
return hex.EncodeToString(h[:])
61
func (h InfoHash) String() string {