From 955d3a96f21d72300810da8c9ccd607196ef1f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Thu, 13 Jul 2023 13:58:53 +0800 Subject: [PATCH] =?UTF-8?q?enhance(hash):=E5=8C=85=E8=A3=85=E6=95=A3?= =?UTF-8?q?=E5=88=97=E7=AE=97=E6=B3=95=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash/crc16/crc16.go | 5 +++-- hash/crc32/crc32.go | 5 +++-- hash/crc64/crc64.go | 5 +++-- hash/crc8/crc8.go | 5 +++-- hash/md5/md5.go | 5 +++-- hash/phash/phash.go | 5 +++-- hash/sha1/sha1.go | 5 +++-- hash/sha256/sha256.go | 5 +++-- hash/sha512/sha512.go | 5 +++-- 9 files changed, 27 insertions(+), 18 deletions(-) diff --git a/hash/crc16/crc16.go b/hash/crc16/crc16.go index b2bbe32..b27a9bb 100644 --- a/hash/crc16/crc16.go +++ b/hash/crc16/crc16.go @@ -3,6 +3,7 @@ package crc16 import ( "encoding/hex" + "fmt" "io" "os" @@ -44,14 +45,14 @@ func CRC16Hex(data []byte, table ...string) string { func SumFile(file string, table ...string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() crcTable := append(table, "IBM") hasher := hasherSelect(crcTable[0]) if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil } diff --git a/hash/crc32/crc32.go b/hash/crc32/crc32.go index fc405de..b86a0dd 100644 --- a/hash/crc32/crc32.go +++ b/hash/crc32/crc32.go @@ -3,6 +3,7 @@ package crc32 import ( "encoding/hex" + "fmt" "hash/crc32" "io" "os" @@ -39,14 +40,14 @@ func CRC32Hex(data []byte, table ...string) string { func SumFile(file string, table ...string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() crcTable := append(table, "IEEE") hasher := crc32.New(tableSelect(crcTable[0])) if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil } diff --git a/hash/crc64/crc64.go b/hash/crc64/crc64.go index 19a9f7e..973457c 100644 --- a/hash/crc64/crc64.go +++ b/hash/crc64/crc64.go @@ -3,6 +3,7 @@ package crc64 import ( "encoding/hex" + "fmt" "hash/crc64" "io" "os" @@ -37,14 +38,14 @@ func CRC64Hex(data []byte, table ...string) string { func SumFile(file string, table ...string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() crcTable := append(table, "ISO") hasher := crc64.New(tableSelect(crcTable[0])) if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil } diff --git a/hash/crc8/crc8.go b/hash/crc8/crc8.go index 57b46ab..3e13262 100644 --- a/hash/crc8/crc8.go +++ b/hash/crc8/crc8.go @@ -3,6 +3,7 @@ package crc8 import ( "encoding/hex" + "fmt" "os" "github.com/sigurn/crc8" @@ -51,14 +52,14 @@ func CRC8Hex(data []byte, table ...string) string { func SumFile(file string, table ...string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() crcTable := append(table, "CRC8") var buf = make([]byte, 0) if _, err := f.Read(buf); err != nil { - return nil, err + return nil, fmt.Errorf("读取指定文件内容出错,%w", err) } return []byte{crc8.Checksum(buf, hasherSelect(crcTable[0]))}, nil } diff --git a/hash/md5/md5.go b/hash/md5/md5.go index 3e39a70..33218df 100644 --- a/hash/md5/md5.go +++ b/hash/md5/md5.go @@ -4,6 +4,7 @@ package md5 import ( "crypto/md5" "encoding/hex" + "fmt" "io" "os" ) @@ -24,13 +25,13 @@ func MD5Hex(data []byte) string { func SumFile(file string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() hasher := md5.New() if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil } diff --git a/hash/phash/phash.go b/hash/phash/phash.go index c173619..a97acb1 100644 --- a/hash/phash/phash.go +++ b/hash/phash/phash.go @@ -4,6 +4,7 @@ package phash import ( "encoding/binary" "encoding/hex" + "fmt" "image" "os" @@ -27,13 +28,13 @@ func HashHex(image image.Image) string { func HashFile(file string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() img, _, err := image.Decode(f) if err != nil { - return nil, err + return nil, fmt.Errorf("未能解码指定图像文件,%w", err) } return Hash(img), nil } diff --git a/hash/sha1/sha1.go b/hash/sha1/sha1.go index 88ae85e..4755631 100644 --- a/hash/sha1/sha1.go +++ b/hash/sha1/sha1.go @@ -4,6 +4,7 @@ package sha1 import ( "crypto/sha1" "encoding/hex" + "fmt" "io" "os" ) @@ -24,13 +25,13 @@ func Sha1Hex(data []byte) string { func SumFile(file string) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() hasher := sha1.New() if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil } diff --git a/hash/sha256/sha256.go b/hash/sha256/sha256.go index 7ca96a1..aa26dc0 100644 --- a/hash/sha256/sha256.go +++ b/hash/sha256/sha256.go @@ -4,6 +4,7 @@ package sha256 import ( "crypto/sha256" "encoding/hex" + "fmt" "hash" "io" "os" @@ -60,13 +61,13 @@ func Sum256Hex(data []byte, bitSize ...int) string { func SumFile256(file string, bitSize ...int) ([]byte, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() hasher := hasherSelect(bitSize[0]) if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil } diff --git a/hash/sha512/sha512.go b/hash/sha512/sha512.go index 0fe7395..759514f 100644 --- a/hash/sha512/sha512.go +++ b/hash/sha512/sha512.go @@ -4,6 +4,7 @@ package sha512 import ( "crypto/sha512" "encoding/hex" + "fmt" "hash" "io" "os" @@ -76,14 +77,14 @@ func Sum512Hex(data []byte, bitSize ...int) string { func SumFile512(path string, bitSize ...int) ([]byte, error) { f, err := os.Open(path) if err != nil { - return nil, err + return nil, fmt.Errorf("未能打开指定文件,%w", err) } defer f.Close() length := append(bitSize, 512) var hasher = hasherSelect(length[0]) if _, err := io.Copy(hasher, f); err != nil { - return nil, err + return nil, fmt.Errorf("未能读取指定文件的内容,%w", err) } return hasher.Sum(nil), nil }