aboutsummaryrefslogtreecommitdiff
path: root/core/crypt.go
blob: f741b8e28ca3e730781a348ea57f97f7a2bfc168 (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 (
	"bytes"
	"crypto/sha256"
	"fmt"
	"io"
	"strings"
)

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

func CalculateBlockHash(block []byte) (string, error) {
	hash := sha256.New()
	if _, err := io.Copy(hash, bytes.NewReader(block)); err != nil {
		return "", err
	}
	hashInBytes := hash.Sum(nil)[:32]
	return strings.TrimSpace(fmt.Sprintf("%x", hashInBytes)), nil
}