aboutsummaryrefslogtreecommitdiff
path: root/hash.go
blob: f03866f1fd3bc27ed3446dc346a86cbc2c71f74b (about) (plain) (blame)
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
package furgit

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
)

// To change the hash algorithm you probably only need to change these two lines...

const HashSize = sha1.Size

var newHash = sha1.Sum

// Hash represents a Git object identifier.
type Hash [HashSize]byte

// ParseHash converts a hex string into an Hash.
func ParseHash(s string) (Hash, error) {
	var id Hash
	if len(s) != HashSize*2 {
		return id, fmt.Errorf("furgit: invalid hash length %d", len(s))
	}
	data, err := hex.DecodeString(s)
	if err != nil {
		return id, fmt.Errorf("furgit: decode hash: %w", err)
	}
	copy(id[:], data)
	return id, nil
}

// String renders the ID as hex.
func (id Hash) String() string {
	return hex.EncodeToString(id[:])
}

// Bytes returns a mutable copy of the underlying bytes.
func (id Hash) Bytes() []byte {
	return append([]byte(nil), id[:]...)
}