Skip to content

Commit 1bcac8c

Browse files
Merge pull request #42 from PDOK/hashing-from-strings
feat: hash from a slice of strings
2 parents 7db9240 + 29df8e6 commit 1bcac8c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/util/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ package controller
2626

2727
import (
2828
"context"
29+
"crypto/sha256"
30+
"encoding/hex"
2931
"encoding/json"
3032
"fmt"
3133
"maps"
@@ -220,6 +222,22 @@ func KustomizeHash(obj client.Object) (hash string, err error) {
220222
return kustomizeHasher.Hash(objKYaml)
221223
}
222224

225+
// GenerateHashFromStrings generates a hash from a list of strings. This hash can be used to identify resources and
226+
// if they can be removed. E.g. the ogcapi-operator provides the volume-operator with a hash so it knows what resources
227+
// to create or remove.
228+
func GenerateHashFromStrings(sliceToHash []string) string {
229+
// Concatenate all strings to hash
230+
var data string
231+
232+
for _, val := range sliceToHash {
233+
data += val
234+
}
235+
236+
hash := sha256.Sum256([]byte(data))
237+
// Take the first 8 bytes of the hash to make it in the same format as the kustomize hash.
238+
return hex.EncodeToString(hash[:8])
239+
}
240+
223241
func EnsureSetGVK(c client.Client, src client.Object, obj schema.ObjectKind) error {
224242
gvk, err := c.GroupVersionKindFor(src)
225243
if err != nil {

0 commit comments

Comments
 (0)