aboutsummaryrefslogblamecommitdiffstats
path: root/core/hash.go
blob: c50ffb25a716983f096c53d66330fb7264fc4518 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
            



                      
              





                          


                                   

                                                                  


                                              











                                                                   




                             


                                       

















                                                    
package core

import (
	"crypto/sha1"
	"encoding/hex"
	"hash"
	"strconv"
)

// Hash SHA1 hased content
type Hash [20]byte

// ZeroHash is Hash with value zero
var ZeroHash Hash

// ComputeHash compute the hash for a given ObjectType and content
func ComputeHash(t ObjectType, content []byte) Hash {
	h := NewHasher(t, int64(len(content)))
	h.Write(content)
	return h.Sum()
}

// NewHash return a new Hash from a hexadecimal hash representation
func NewHash(s string) Hash {
	b, _ := hex.DecodeString(s)

	var h Hash
	copy(h[:], b)

	return h
}

func (h Hash) IsZero() bool {
	var empty Hash
	return h == empty
}

func (h Hash) String() string {
	return hex.EncodeToString(h[:])
}

type Hasher struct {
	hash.Hash
}

func NewHasher(t ObjectType, size int64) Hasher {
	h := Hasher{sha1.New()}
	h.Write(t.Bytes())
	h.Write([]byte(" "))
	h.Write([]byte(strconv.FormatInt(size, 10)))
	h.Write([]byte{0})
	return h
}

func (h Hasher) Sum() (hash Hash) {
	copy(hash[:], h.Hash.Sum(nil))
	return
}