-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core/filemgr: use xxhash instead of sha512 for filenames
- Loading branch information
1 parent
53573dc
commit 9ac7f53
Showing
6 changed files
with
64 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package filemgr | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/cespare/xxhash/v2" | ||
"github.com/martinlindhe/base36" | ||
) | ||
|
||
// GetFileNameWithBytesHash constructs a filename using a base filename and a hash of | ||
// the data. For example: GetFileNameWithBytesHash("example.txt", []byte{...}) ==> "example-abcd1234.txt" | ||
func GetFileNameWithBytesHash(base string, data []byte) string { | ||
h := xxhash.Sum64(data) | ||
he := base36.Encode(h) | ||
ext := filepath.Ext(base) | ||
return fmt.Sprintf("%s-%x%s", base[:len(base)-len(ext)], he, ext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package filemgr | ||
|
||
import ( | ||
"crypto/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkGetFileNameWithBytesHash(b *testing.B) { | ||
bs := make([]byte, 1024*128) | ||
_, err := rand.Read(bs) | ||
require.NoError(b, err) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
GetFileNameWithBytesHash("example.crt", bs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters