diff --git a/crypto/md5.go b/crypto/md5.go index 7ff59e8..4550193 100644 --- a/crypto/md5.go +++ b/crypto/md5.go @@ -1,8 +1,12 @@ package crypto import ( + "bufio" "crypto/md5" "encoding/hex" + "fmt" + "io" + "os" "strings" ) @@ -17,3 +21,35 @@ func (c *Crypto) Md5Encode(data string) string { h.Write([]byte(data)) return hex.EncodeToString(h.Sum(nil)) } + +// Md5File return the md5 value of file +func (c *Crypto) Md5EncodeFile(filename string) (string, error) { + if fileInfo, err := os.Stat(filename); err != nil { + return "", err + } else if fileInfo.IsDir() { + return "", nil + } + + file, err := os.Open(filename) + if err != nil { + return "", err + } + defer file.Close() + + hash := md5.New() + + chunkSize := 65536 + for buf, reader := make([]byte, chunkSize), bufio.NewReader(file); ; { + n, err := reader.Read(buf) + if err != nil { + if err == io.EOF { + break + } + return "", err + } + hash.Write(buf[:n]) + } + + checksum := fmt.Sprintf("%x", hash.Sum(nil)) + return checksum, nil +} diff --git a/crypto/md5_test.go b/crypto/md5_test.go index 754e566..f9183ed 100644 --- a/crypto/md5_test.go +++ b/crypto/md5_test.go @@ -14,3 +14,9 @@ func TestMd5(tt *testing.T) { //true } + +func TestCrypto_Md5EncodeFile(t *testing.T) { + filePath := "./md5.go" + fmt.Println(cryptoTool.Md5EncodeFile(filePath)) + //fea07bd54d5c1841a682b408fbbb0cf8 +} diff --git a/str/bit62_test.go b/str/bit62_test.go index a4699f4..7f47afc 100644 --- a/str/bit62_test.go +++ b/str/bit62_test.go @@ -29,3 +29,10 @@ func TestBit62Add(t *testing.T) { fmt.Println(StrTool.Bit62Add("ly7vp", "1")) } + +func FuzzStr_Bit62Encode(f *testing.F) { + f.Fuzz(func(t *testing.T, a int64) { + fmt.Println(a) + fmt.Println(StrTool.Bit62Encode(a)) + }) +}