summaryrefslogtreecommitdiff
path: root/shelf/hash.go
blob: eea81d5885fa8fd0aa8655c8487fc91f7fa0404c (plain)
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
package shelf

import (
	"crypto/sha256"
	"encoding/hex"
	"io"
	"os"
)

func hashFile(name string) (string, error) {
	var file *os.File

	if f, err := os.Open(name); err != nil {
		return "", err
	} else {
		file = f
	}

	defer file.Close()

	hash := sha256.New()

	if _, err := io.Copy(hash, file); err != nil {
		return "", err
	}

	return hex.EncodeToString(hash.Sum(nil)), nil
}