下面是GoLang 计算小文件或大文件 md5 值的例子

GoLang 计算小文件和大文件 md5 值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
    "crypto/md5"
    "fmt"
    "io"
    "os"
)

func main() {
    testFile := "/path/to/file"
    file, err := os.Open(testFile)
    if err != nil {
        fmt.Println(err)
        return
    }
    md5h := md5.New()
    io.Copy(md5h, file)
    fmt.Printf("%x", md5h.Sum([]byte(""))) //md5
}

如果是大文件,可以分块计算,参见下面例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main

import (
    "crypto/md5"
    "fmt"
    "io"
    "math"
    "os"
)

const filechunk = 8192 // we settle for 8KB

func main() {

    file, err := os.Open("utf8.txt")

    if err != nil {
        panic(err.Error())
    }

    defer file.Close()

    // calculate the file size
    info, _ := file.Stat()

    filesize := info.Size()

    blocks := uint64(math.Ceil(float64(filesize) / float64(filechunk)))

    hash := md5.New()

    for i := uint64(0); i < blocks; i++ {
        blocksize := int(math.Min(filechunk, float64(filesize-int64(i*filechunk))))
        buf := make([]byte, blocksize)

        file.Read(buf)
        io.WriteString(hash, string(buf)) // append into the hash
    }

    fmt.Printf("%s checksum is %x\n", file.Name(), hash.Sum(nil))

}

本文网址: https://golangnote.com/topic/39.html 转摘请注明来源