Skip to content

Commit ff0db37

Browse files
committed
change generate hash
1 parent 380f665 commit ff0db37

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

pkg/files/file_helpers.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ package files
88

99
import (
1010
"cmp"
11+
"crypto/sha256"
1112
"crypto/x509"
13+
"encoding/base64"
1214
"fmt"
1315
"net"
1416
"os"
1517
"slices"
1618
"strconv"
1719

18-
"github.com/google/uuid"
19-
2020
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
2121
"github.com/nginx/agent/v3/internal/datasource/cert"
2222
"google.golang.org/protobuf/types/known/timestamppb"
@@ -135,7 +135,10 @@ func GenerateConfigVersion(fileSlice []*mpi.File) string {
135135

136136
// GenerateHash returns the hash value of a file's contents.
137137
func GenerateHash(b []byte) string {
138-
return uuid.NewMD5(uuid.Nil, b).String()
138+
hash := sha256.New()
139+
hash.Write(b)
140+
141+
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
139142
}
140143

141144
// ConvertToMapOfFiles converts a list of files to a map of files with the file name as the key

pkg/files/file_helpers_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
package files
77

88
import (
9+
"crypto/sha256"
910
"crypto/x509"
11+
"encoding/base64"
1012
"net"
1113
"os"
1214
"testing"
1315

14-
"github.com/google/uuid"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
1718

@@ -139,6 +140,10 @@ func Test_GenerateConfigVersion(t *testing.T) {
139140
}
140141

141142
func TestGenerateHash(t *testing.T) {
143+
hash1 := sha256.New()
144+
hash2 := sha256.New()
145+
hash1.Write([]byte(""))
146+
hash2.Write([]byte("test"))
142147
tests := []struct {
143148
name string
144149
expected string
@@ -147,12 +152,12 @@ func TestGenerateHash(t *testing.T) {
147152
{
148153
name: "Test 1: empty byte slice",
149154
input: []byte{},
150-
expected: uuid.NewMD5(uuid.Nil, []byte("")).String(),
155+
expected: base64.StdEncoding.EncodeToString(hash1.Sum(nil)),
151156
},
152157
{
153158
name: "Test 2: non-empty byte slice",
154159
input: []byte("test"),
155-
expected: uuid.NewMD5(uuid.Nil, []byte("test")).String(),
160+
expected: base64.StdEncoding.EncodeToString(hash2.Sum(nil)),
156161
},
157162
}
158163

0 commit comments

Comments
 (0)