aboutsummaryrefslogtreecommitdiffhomepage
path: root/common/hashing/hashing.go
blob: abf8e6b143164e3c1c2bfb04afd0068a2df37a5e (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package hashing provides common hashing utilities.
package hashing

import (
	"encoding/hex"
	"io"
	"sync"

	"github.com/cespare/xxhash/v2"
)

// XXHashFromReader calculates the xxHash for the given reader.
func XXHashFromReader(r io.ReadSeeker) (uint64, int64, error) {
	h := getXxHashReadFrom()
	defer putXxHashReadFrom(h)

	size, err := io.Copy(h, r)
	if err != nil {
		return 0, 0, err
	}
	return h.Sum64(), size, nil
}

// XXHashFromString calculates the xxHash for the given string.
func XXHashFromString(s string) (uint64, error) {
	h := xxhash.New()
	h.WriteString(s)
	return h.Sum64(), nil
}

// XxHashFromStringHexEncoded calculates the xxHash for the given string
// and returns the hash as a hex encoded string.
func XxHashFromStringHexEncoded(f string) string {
	h := xxhash.New()
	h.WriteString(f)
	hash := h.Sum(nil)
	return hex.EncodeToString(hash)
}

type xxhashReadFrom struct {
	buff []byte
	*xxhash.Digest
}

func (x *xxhashReadFrom) ReadFrom(r io.Reader) (int64, error) {
	for {
		n, err := r.Read(x.buff)
		if n > 0 {
			x.Digest.Write(x.buff[:n])
		}
		if err != nil {
			if err == io.EOF {
				err = nil
			}
			return int64(n), err
		}
	}
}

var xXhashReadFromPool = sync.Pool{
	New: func() any {
		return &xxhashReadFrom{Digest: xxhash.New(), buff: make([]byte, 48*1024)}
	},
}

func getXxHashReadFrom() *xxhashReadFrom {
	return xXhashReadFromPool.Get().(*xxhashReadFrom)
}

func putXxHashReadFrom(h *xxhashReadFrom) {
	h.Reset()
	xXhashReadFromPool.Put(h)
}