blob: 068eb84017b2d0766b91000acab92cae6b937ea4 (
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
41
42
43
44
45
46
47
48
49
50
51
|
package furgit
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
)
const maxHashSize = 32
// Hash represents a Git object ID.
type Hash struct {
data [maxHashSize]byte
size int
}
// hashFunc is a function that computes a hash from input data.
type hashFunc func([]byte) Hash
// hashFuncs maps hash size to hash function.
var hashFuncs = map[int]hashFunc{
sha1.Size: func(data []byte) Hash {
sum := sha1.Sum(data)
var h Hash
copy(h.data[:], sum[:])
h.size = sha1.Size
return h
},
sha256.Size: func(data []byte) Hash {
sum := sha256.Sum256(data)
var h Hash
copy(h.data[:], sum[:])
h.size = sha256.Size
return h
},
}
// String returns a hexadecimal string representation of the hash.
func (hash Hash) String() string {
return hex.EncodeToString(hash.data[:hash.size])
}
// Bytes returns a copy of the hash's bytes.
func (hash Hash) Bytes() []byte {
return append([]byte(nil), hash.data[:hash.size]...)
}
// Size returns the hash size.
func (hash Hash) Size() int {
return hash.size
}
|