|
|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
|