From e7fa4835f71f47ab1d13afd638cebf661800d5a4 Mon Sep 17 00:00:00 2001 From: Stone <73482944+Stone-afk@users.noreply.github.com> Date: Sun, 8 Dec 2024 09:30:41 -0500 Subject: [PATCH] modify: file cache writer md5 to sha256 (#5727) * modify: file cache writer md5 to sha256 * modify: file cache writer md5 to sha256 * modify: file cache writer md5 to sha256 --- client/cache/file.go | 14 ++++++++------ client/cache/file_test.go | 10 ++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/client/cache/file.go b/client/cache/file.go index d2b4a774..e665cdc1 100644 --- a/client/cache/file.go +++ b/client/cache/file.go @@ -17,7 +17,7 @@ package cache import ( "bytes" "context" - "crypto/md5" + "crypto/sha256" "encoding/gob" "encoding/hex" "encoding/json" @@ -123,21 +123,22 @@ func (fc *FileCache) Init() error { // getCacheFileName returns a md5 encoded file name. func (fc *FileCache) getCacheFileName(key string) (string, error) { - m := md5.New() + m := sha256.New() _, _ = io.WriteString(m, key) - keyMd5 := hex.EncodeToString(m.Sum(nil)) + keySha256 := hex.EncodeToString(m.Sum(nil)) cachePath := fc.CachePath switch fc.DirectoryLevel { case 2: - cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4]) + cachePath = filepath.Join(cachePath, keySha256[0:2], keySha256[2:4]) case 1: - cachePath = filepath.Join(cachePath, keyMd5[0:2]) + cachePath = filepath.Join(cachePath, keySha256[0:2]) } ok, err := exists(cachePath) if err != nil { return "", err } if !ok { + fmt.Printf("cachePath: %s\n", cachePath) err = os.MkdirAll(cachePath, os.ModePerm) if err != nil { return "", berror.Wrapf(err, CreateFileCacheDirFailed, @@ -145,7 +146,7 @@ func (fc *FileCache) getCacheFileName(key string) (string, error) { } } - return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, fc.FileSuffix)), nil + return filepath.Join(cachePath, fmt.Sprintf("%s%s", keySha256, fc.FileSuffix)), nil } // Get value from file cache. @@ -212,6 +213,7 @@ func (fc *FileCache) Put(ctx context.Context, key string, val interface{}, timeo } fn, err := fc.getCacheFileName(key) + if err != nil { return err } diff --git a/client/cache/file_test.go b/client/cache/file_test.go index f9a325d6..db2b8ada 100644 --- a/client/cache/file_test.go +++ b/client/cache/file_test.go @@ -17,11 +17,10 @@ package cache import ( "context" "fmt" + "github.com/stretchr/testify/assert" "os" "path/filepath" "testing" - - "github.com/stretchr/testify/assert" ) func TestFileCacheStartAndGC(t *testing.T) { @@ -30,6 +29,8 @@ func TestFileCacheStartAndGC(t *testing.T) { assert.NotNil(t, err) err = fc.StartAndGC(`{}`) assert.Nil(t, err) + _, err = fc.getCacheFileName("key1") + assert.Nil(t, err) assert.Equal(t, fc.CachePath, FileCachePath) assert.Equal(t, fc.DirectoryLevel, FileCacheDirectoryLevel) @@ -47,12 +48,17 @@ func TestFileCacheStartAndGC(t *testing.T) { assert.Equal(t, fc.DirectoryLevel, 2) assert.Equal(t, fc.EmbedExpiry, 0) assert.Equal(t, fc.FileSuffix, ".bin") + _, err = fc.getCacheFileName("key1") + assert.Nil(t, err) err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"aaa","EmbedExpiry":"0"}`, str)) assert.NotNil(t, err) err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"aaa"}`, str)) assert.NotNil(t, err) + + _, err = fc.getCacheFileName("key1") + assert.Nil(t, err) } func TestFileCacheInit(t *testing.T) {