diff --git a/pkg/files/file_helpers.go b/pkg/files/file_helpers.go index ad89d4efa..ff286485f 100644 --- a/pkg/files/file_helpers.go +++ b/pkg/files/file_helpers.go @@ -8,15 +8,15 @@ package files import ( "cmp" + "crypto/sha256" "crypto/x509" + "encoding/base64" "fmt" "net" "os" "slices" "strconv" - "github.com/google/uuid" - mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" "github.com/nginx/agent/v3/internal/datasource/cert" "google.golang.org/protobuf/types/known/timestamppb" @@ -135,7 +135,10 @@ func GenerateConfigVersion(fileSlice []*mpi.File) string { // GenerateHash returns the hash value of a file's contents. func GenerateHash(b []byte) string { - return uuid.NewMD5(uuid.Nil, b).String() + hash := sha256.New() + hash.Write(b) + + return base64.StdEncoding.EncodeToString(hash.Sum(nil)) } // ConvertToMapOfFiles converts a list of files to a map of files with the file name as the key diff --git a/pkg/files/file_helpers_test.go b/pkg/files/file_helpers_test.go index 2000d1567..75dcd9762 100644 --- a/pkg/files/file_helpers_test.go +++ b/pkg/files/file_helpers_test.go @@ -6,12 +6,13 @@ package files import ( + "crypto/sha256" "crypto/x509" + "encoding/base64" "net" "os" "testing" - "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -136,6 +137,10 @@ func Test_GenerateConfigVersion(t *testing.T) { } func TestGenerateHash(t *testing.T) { + hash1 := sha256.New() + hash2 := sha256.New() + hash1.Write([]byte("")) + hash2.Write([]byte("test")) tests := []struct { name string expected string @@ -144,12 +149,12 @@ func TestGenerateHash(t *testing.T) { { name: "Test 1: empty byte slice", input: []byte{}, - expected: uuid.NewMD5(uuid.Nil, []byte("")).String(), + expected: base64.StdEncoding.EncodeToString(hash1.Sum(nil)), }, { name: "Test 2: non-empty byte slice", input: []byte("test"), - expected: uuid.NewMD5(uuid.Nil, []byte("test")).String(), + expected: base64.StdEncoding.EncodeToString(hash2.Sum(nil)), }, }