aboutsummaryrefslogtreecommitdiff
path: root/core/crypt.go
blob: 6de70e62654773c3c1f02c18e8615eab84866f11 (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
package core

import (
	"crypto/sha256"
	"fmt"
	"io"
	"os"
	"strings"
)

func calculateStringHash(a string) (string, error) {
	hash := sha256.New()
	hash.Write([]byte(a))
	hashInBytes := hash.Sum(nil)[:20]
	return strings.TrimSpace(fmt.Sprintf("%x", hashInBytes)), nil
}

func calculateFileHash(file *os.File) (string, error) {
	hash := sha256.New()
	if _, err := io.Copy(hash, file); err != nil {
		return "", err
	}
	hashInBytes := hash.Sum(nil)[:20]
	return strings.TrimSpace(fmt.Sprintf("%x", hashInBytes)), nil
}