sk-matrix-project/app/wails/pkg/sdk/file/hash/hash.go
2023-07-24 10:46:23 +08:00

59 lines
1.2 KiB
Go

package hash
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
fileSdk "skapp/pkg/sdk/file"
)
type Support struct {
fileSupport *fileSdk.Support
}
func New(fileSupport *fileSdk.Support) *Support {
return &Support{fileSupport}
}
func (s *Support) CalcSHA512(path string, readSize int64) (hash string, err error) {
h := sha512.New()
_, err = s.fileSupport.CopyN(h, path, readSize)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func (s *Support) CalcSHA256(path string, readSize int64) (hash string, err error) {
h := sha256.New()
_, err = s.fileSupport.CopyN(h, path, readSize)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func (s *Support) CalcSHA1(path string, readSize int64) (hash string, err error) {
h := sha1.New()
_, err = s.fileSupport.CopyN(h, path, readSize)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func (s *Support) CalcMD5(path string, readSize int64) (hash string, err error) {
h := md5.New()
_, err = s.fileSupport.CopyN(h, path, readSize)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}