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
|
package main
import (
"crypto/sha512"
"fmt"
"io"
"os"
)
func checkFile(filepath string, checksum string) (bool, error) {
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return false, nil
}
// file exists, check checksum
lsum, err := hash(filepath)
if err != nil {
return false, err
}
if checksum != fmt.Sprintf("%x", lsum) {
return false, nil
}
return true, nil
}
func hash(filepath string) ([]byte, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
hasher := sha512.New()
if _, err := io.Copy(hasher, file); err != nil {
return nil, err
}
return hasher.Sum(nil), nil
}
|