mirror of
https://github.com/pocket-id/pocket-id.git
synced 2025-12-10 09:13:31 +03:00
31 lines
533 B
Go
31 lines
533 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func CreateSha256Hash(input string) string {
|
|
hash := sha256.Sum256([]byte(input))
|
|
return hex.EncodeToString(hash[:])
|
|
}
|
|
|
|
func CreateSha256FileHash(filePath string) ([]byte, error) {
|
|
f, err := os.Open(filePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open file: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
h := sha256.New()
|
|
_, err = io.Copy(h, f)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read file: %w", err)
|
|
}
|
|
|
|
return h.Sum(nil), nil
|
|
}
|