aboutsummaryrefslogtreecommitdiff
path: root/utils.go
blob: cc99d93a5b8b7125af1634c9d0109697ce5320db (plain) (blame)
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
package main

import (
	"crypto/sha256"
	"io"
	"log"
	"os"
)

const invalidHashString = "invalid"

func Sha256Sum(fileName string) (string, error) {
	f, err := os.Open(fileName)
	if err != nil {
		return invalidHashString, err
	}
	defer func() {
		err = f.Close()
		if err != nil {
			log.Println("Cannot close file " + fileName)
		}
	}()

	h := sha256.New()
	if _, err := io.Copy(h, f); err != nil {
		return invalidHashString, err
	}

	return string(h.Sum(nil)), nil
}