Skip to content

Instantly share code, notes, and snippets.

@twsiyuan
Last active April 7, 2017 03:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save twsiyuan/14320ea77778b6696ab06da26ff7374a to your computer and use it in GitHub Desktop.
Calculate md5 from file chunk using golang
package main
import (
"crypto/md5"
"flag"
"fmt"
"io"
"os"
)
const (
bufferSize = int64(8 * 1024 * 1024)
)
func main() {
var filePath string
var chunkSize int64
flag.StringVar(&filePath, "filepath", "", "file path")
flag.Int64Var(&chunkSize, "chunksize", -1, "chunk size in bytes")
flag.Parse()
if len(filePath) <= 0 {
println("No filepath")
os.Exit(1)
}
fileInfo, err := os.Stat(filePath)
if err != nil {
println("Cannot find file,", filePath, ", error:", err.Error())
os.Exit(1)
}
chunkCount := int64(1)
if chunkSize > 0 {
chunkCount = (fileInfo.Size() / chunkSize) + 1
} else {
chunkSize = fileInfo.Size()
}
file, err := os.Open(filePath)
if err != nil {
println("Cannot open file,", filePath, ", error:", err.Error())
os.Exit(1)
}
defer file.Close()
hash := md5.New()
buff := make([]byte, bufferSize)
for i := int64(0); i < chunkCount; i++ {
hash.Reset()
for chunkReaded := int64(0); chunkReaded < chunkSize; {
m := bufferSize
if chunkReaded+m > chunkSize {
m = chunkSize - chunkReaded
}
n, err := file.Read(buff[0:m])
if err != nil {
if err == io.EOF {
break
}
println("Cannot read file,", filePath, ", error:", err.Error())
os.Exit(1)
}
hash.Write(buff[0:n])
chunkReaded += int64(n)
}
if chunkCount > 1 {
fmt.Printf("Chunk[%d] md5 = %x\r\n", i, hash.Sum(nil))
} else {
fmt.Printf("md5 = %x\r\n", hash.Sum(nil))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment