2023-07-24 10:40:12 +08:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2023-07-24 10:46:23 +08:00
|
|
|
func (s *Support) CalcSHA512(path string, readSize int64) (hash string, err error) {
|
2023-07-24 10:40:12 +08:00
|
|
|
h := sha512.New()
|
|
|
|
_, err = s.fileSupport.CopyN(h, path, readSize)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 10:46:23 +08:00
|
|
|
func (s *Support) CalcSHA256(path string, readSize int64) (hash string, err error) {
|
2023-07-24 10:40:12 +08:00
|
|
|
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
|
|
|
|
}
|