github.com/argoproj/argo-cd/v3@v3.2.1/util/util.go (about) 1 package util 2 3 import ( 4 "crypto/rand" 5 "crypto/sha256" 6 "encoding/base64" 7 "encoding/hex" 8 "fmt" 9 10 "k8s.io/apimachinery/pkg/runtime" 11 ) 12 13 // MakeSignature generates a cryptographically-secure pseudo-random token, based on a given number of random bytes, for signing purposes. 14 func MakeSignature(size int) ([]byte, error) { 15 b := make([]byte, size) 16 _, err := rand.Read(b) 17 if err != nil { 18 b = nil 19 } 20 // base64 encode it so signing key can be typed into validation utilities 21 b = []byte(base64.StdEncoding.EncodeToString(b)) 22 return b, err 23 } 24 25 // SliceCopy generates a deep copy of a slice containing any type that implements the runtime.Object interface. 26 func SliceCopy[T runtime.Object](items []T) []T { 27 itemsCopy := make([]T, len(items)) 28 for i, item := range items { 29 itemsCopy[i] = item.DeepCopyObject().(T) 30 } 31 return itemsCopy 32 } 33 34 // GenerateCacheKey generates a cache key based on a format string and arguments 35 func GenerateCacheKey(format string, args ...any) (string, error) { 36 h := sha256.New() 37 _, err := fmt.Fprintf(h, format, args...) 38 if err != nil { 39 return "", err 40 } 41 42 key := hex.EncodeToString(h.Sum(nil)) 43 return key, nil 44 }