enhance(hash):包装散列算法中的错误提示。
This commit is contained in:
		@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user