2021-05-05 05:06:05 +07:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2021-08-03 16:55:21 +07:00
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-05-05 05:06:05 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// HashSHA256FromString takes an input string and calculates the SHA256 checksum returning it as a base16 hash string.
|
|
|
|
func HashSHA256FromString(input string) (output string) {
|
2021-08-03 16:55:21 +07:00
|
|
|
hash := sha256.New()
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
hash.Write([]byte(input))
|
|
|
|
|
|
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashSHA256FromPath takes a path string and calculates the SHA256 checksum of the file at the path returning it as a base16 hash string.
|
|
|
|
func HashSHA256FromPath(path string) (output string, err error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
hash := sha256.New()
|
|
|
|
|
|
|
|
if _, err := io.Copy(hash, file); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(hash.Sum(nil)), nil
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|