| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 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 |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package hashing provides common hashing utilities. |
| 15 |
package hashing |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/md5" |
| 19 |
"encoding/hex" |
| 20 |
"io" |
| 21 |
"reflect" |
| 22 |
"strconv" |
| 23 |
"sync" |
| 24 |
|
| 25 |
"github.com/cespare/xxhash/v2" |
| 26 |
"github.com/gohugoio/hashstructure" |
| 27 |
"github.com/gohugoio/hugo/common/hugio" |
| 28 |
"github.com/gohugoio/hugo/identity" |
| 29 |
) |
| 30 |
|
| 31 |
// XXHashFromReader calculates the xxHash for the given reader. |
| 32 |
func XXHashFromReader(r io.Reader) (uint64, int64, error) { |
| 33 |
h := getXxHashReadFrom() |
| 34 |
defer putXxHashReadFrom(h) |
| 35 |
|
| 36 |
size, err := io.Copy(h, r) |
| 37 |
if err != nil { |
| 38 |
return 0, 0, err |
| 39 |
} |
| 40 |
return h.Sum64(), size, nil |
| 41 |
} |
| 42 |
|
| 43 |
type Hasher interface { |
| 44 |
io.StringWriter |
| 45 |
io.Writer |
| 46 |
io.ReaderFrom |
| 47 |
Sum64() uint64 |
| 48 |
} |
| 49 |
|
| 50 |
type HashCloser interface { |
| 51 |
Hasher |
| 52 |
io.Closer |
| 53 |
} |
| 54 |
|
| 55 |
// XxHasher returns a Hasher that uses xxHash. |
| 56 |
// Remember to call Close when done. |
| 57 |
func XxHasher() HashCloser { |
| 58 |
h := getXxHashReadFrom() |
| 59 |
return struct { |
| 60 |
Hasher |
| 61 |
io.Closer |
| 62 |
}{ |
| 63 |
Hasher: h, |
| 64 |
Closer: hugio.CloserFunc(func() error { |
| 65 |
putXxHashReadFrom(h) |
| 66 |
return nil |
| 67 |
}), |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// XxHashFromReaderHexEncoded calculates the xxHash for the given reader |
| 72 |
// and returns the hash as a hex encoded string. |
| 73 |
func XxHashFromReaderHexEncoded(r io.Reader) (string, error) { |
| 74 |
h := getXxHashReadFrom() |
| 75 |
defer putXxHashReadFrom(h) |
| 76 |
_, err := io.Copy(h, r) |
| 77 |
if err != nil { |
| 78 |
return "", err |
| 79 |
} |
| 80 |
hash := h.Sum(nil) |
| 81 |
return hex.EncodeToString(hash), nil |
| 82 |
} |
| 83 |
|
| 84 |
// XXHashFromString calculates the xxHash for the given string. |
| 85 |
func XXHashFromString(s string) (uint64, error) { |
| 86 |
h := xxhash.New() |
| 87 |
h.WriteString(s) |
| 88 |
return h.Sum64(), nil |
| 89 |
} |
| 90 |
|
| 91 |
// XxHashFromStringHexEncoded calculates the xxHash for the given strings |
| 92 |
// and returns the hash as a hex encoded string. |
| 93 |
func XxHashFromStringHexEncoded(s ...string) string { |
| 94 |
h := xxhash.New() |
| 95 |
for _, f := range s { |
| 96 |
h.WriteString(f) |
| 97 |
} |
| 98 |
hash := h.Sum(nil) |
| 99 |
return hex.EncodeToString(hash) |
| 100 |
} |
| 101 |
|
| 102 |
// MD5FromStringHexEncoded returns the MD5 hash of the given string. |
| 103 |
func MD5FromStringHexEncoded(f string) string { |
| 104 |
h := md5.New() |
| 105 |
h.Write([]byte(f)) |
| 106 |
return hex.EncodeToString(h.Sum(nil)) |
| 107 |
} |
| 108 |
|
| 109 |
// MD5FromReaderHexEncoded returns the MD5 hash of the given reader. |
| 110 |
func MD5FromReaderHexEncoded(r io.Reader) string { |
| 111 |
h := md5.New() |
| 112 |
_, err := io.Copy(h, r) |
| 113 |
if err != nil { |
| 114 |
return "" |
| 115 |
} |
| 116 |
return hex.EncodeToString(h.Sum(nil)) |
| 117 |
} |
| 118 |
|
| 119 |
// HashString returns a hash from the given elements. |
| 120 |
// It will panic if the hash cannot be calculated. |
| 121 |
// Note that this hash should be used primarily for identity, not for change detection as |
| 122 |
// it in the more complex values (e.g. Page) will not hash the full content. |
| 123 |
func HashString(vs ...any) string { |
| 124 |
hash := HashUint64(vs...) |
| 125 |
return strconv.FormatUint(hash, 10) |
| 126 |
} |
| 127 |
|
| 128 |
// HashStringHex returns a hash from the given elements as a hex encoded string. |
| 129 |
// See HashString for more information. |
| 130 |
func HashStringHex(vs ...any) string { |
| 131 |
hash := HashUint64(vs...) |
| 132 |
return strconv.FormatUint(hash, 16) |
| 133 |
} |
| 134 |
|
| 135 |
var hashOptsPool = sync.Pool{ |
| 136 |
New: func() any { |
| 137 |
return &hashstructure.HashOptions{ |
| 138 |
Hasher: xxhash.New(), |
| 139 |
UnwrapFunc: unwrapForHashing, |
| 140 |
} |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
// hashstructure only sees exported struct fields, so rewrite known identity types before hashing, |
| 145 |
// e.g. a Resource or Page nested in an options map hashes by its Key. |
| 146 |
func unwrapForHashing(v reflect.Value) (reflect.Value, error) { |
| 147 |
if v.Kind() != reflect.Struct { |
| 148 |
return v, nil |
| 149 |
} |
| 150 |
var in any |
| 151 |
if v.CanAddr() { |
| 152 |
// The common case; pointer receiver methods on a struct |
| 153 |
// reached through a pointer. |
| 154 |
in = v.Addr().Interface() |
| 155 |
} else { |
| 156 |
in = v.Interface() |
| 157 |
} |
| 158 |
switch t := in.(type) { |
| 159 |
case hashstructure.Hashable: |
| 160 |
// Let hashstructure handle it. |
| 161 |
return v, nil |
| 162 |
case keyer: |
| 163 |
return reflect.ValueOf(t.Key()), nil |
| 164 |
case identity.IdentityProvider: |
| 165 |
return reflect.ValueOf(t.GetIdentity()), nil |
| 166 |
} |
| 167 |
return v, nil |
| 168 |
} |
| 169 |
|
| 170 |
func getHashOpts() *hashstructure.HashOptions { |
| 171 |
return hashOptsPool.Get().(*hashstructure.HashOptions) |
| 172 |
} |
| 173 |
|
| 174 |
func putHashOpts(opts *hashstructure.HashOptions) { |
| 175 |
opts.Hasher.Reset() |
| 176 |
hashOptsPool.Put(opts) |
| 177 |
} |
| 178 |
|
| 179 |
// HashUint64 returns a hash from the given elements. |
| 180 |
// It will panic if the hash cannot be calculated. |
| 181 |
// Note that this hash should be used primarily for identity, not for change detection as |
| 182 |
// it in the more complex values (e.g. Page) will not hash the full content. |
| 183 |
func HashUint64(vs ...any) uint64 { |
| 184 |
var o any |
| 185 |
if len(vs) == 1 { |
| 186 |
o = vs[0] |
| 187 |
} else { |
| 188 |
o = vs |
| 189 |
} |
| 190 |
hash, err := Hash(o) |
| 191 |
if err != nil { |
| 192 |
panic(err) |
| 193 |
} |
| 194 |
return hash |
| 195 |
} |
| 196 |
|
| 197 |
// Hash returns a hash from vs. |
| 198 |
func Hash(vs ...any) (uint64, error) { |
| 199 |
hashOpts := getHashOpts() |
| 200 |
defer putHashOpts(hashOpts) |
| 201 |
var v any = vs |
| 202 |
if len(vs) == 1 { |
| 203 |
v = vs[0] |
| 204 |
} |
| 205 |
return hashstructure.Hash(v, hashOpts) |
| 206 |
} |
| 207 |
|
| 208 |
type keyer interface { |
| 209 |
Key() string |
| 210 |
} |
| 211 |
|
| 212 |
type xxhashReadFrom struct { |
| 213 |
buff []byte |
| 214 |
*xxhash.Digest |
| 215 |
} |
| 216 |
|
| 217 |
func (x *xxhashReadFrom) ReadFrom(r io.Reader) (int64, error) { |
| 218 |
for { |
| 219 |
n, err := r.Read(x.buff) |
| 220 |
if n > 0 { |
| 221 |
x.Digest.Write(x.buff[:n]) |
| 222 |
} |
| 223 |
if err != nil { |
| 224 |
if err == io.EOF { |
| 225 |
err = nil |
| 226 |
} |
| 227 |
return int64(n), err |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
var xXhashReadFromPool = sync.Pool{ |
| 233 |
New: func() any { |
| 234 |
return &xxhashReadFrom{Digest: xxhash.New(), buff: make([]byte, 48*1024)} |
| 235 |
}, |
| 236 |
} |
| 237 |
|
| 238 |
func getXxHashReadFrom() *xxhashReadFrom { |
| 239 |
return xXhashReadFromPool.Get().(*xxhashReadFrom) |
| 240 |
} |
| 241 |
|
| 242 |
func putXxHashReadFrom(h *xxhashReadFrom) { |
| 243 |
h.Reset() |
| 244 |
xXhashReadFromPool.Put(h) |
| 245 |
} |