-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.go
61 lines (46 loc) · 1.06 KB
/
helpers.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2019 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package pkg
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
// ValidateChecksum validates a checksum
func ValidateChecksum(file, checksumsFile string) (bool, error) {
b, err := ioutil.ReadFile(checksumsFile)
if err != nil {
return false, err
}
checkSums := string(b)
checksum, err := GetChecksum(file)
if err != nil {
return false, err
}
return strings.Contains(checkSums, checksum), nil
}
// GetChecksum get a checksum
func GetChecksum(file string) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// EnsureDirectory ensures that directory exists
func EnsureDirectory(dirName string, mode int) error {
err := os.MkdirAll(dirName, os.FileMode(mode))
if err == nil || os.IsExist(err) {
return nil
}
return err
}