enhance(hash):包装散列算法中的错误提示。
This commit is contained in:
parent
2e276ca33c
commit
955d3a96f2
|
@ -3,6 +3,7 @@ package crc16
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -44,14 +45,14 @@ func CRC16Hex(data []byte, table ...string) string {
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "IBM")
|
crcTable := append(table, "IBM")
|
||||||
hasher := hasherSelect(crcTable[0])
|
hasher := hasherSelect(crcTable[0])
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package crc32
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -39,14 +40,14 @@ func CRC32Hex(data []byte, table ...string) string {
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "IEEE")
|
crcTable := append(table, "IEEE")
|
||||||
hasher := crc32.New(tableSelect(crcTable[0]))
|
hasher := crc32.New(tableSelect(crcTable[0]))
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package crc64
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"hash/crc64"
|
"hash/crc64"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -37,14 +38,14 @@ func CRC64Hex(data []byte, table ...string) string {
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "ISO")
|
crcTable := append(table, "ISO")
|
||||||
hasher := crc64.New(tableSelect(crcTable[0]))
|
hasher := crc64.New(tableSelect(crcTable[0]))
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package crc8
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/sigurn/crc8"
|
"github.com/sigurn/crc8"
|
||||||
|
@ -51,14 +52,14 @@ func CRC8Hex(data []byte, table ...string) string {
|
||||||
func SumFile(file string, table ...string) ([]byte, error) {
|
func SumFile(file string, table ...string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
crcTable := append(table, "CRC8")
|
crcTable := append(table, "CRC8")
|
||||||
var buf = make([]byte, 0)
|
var buf = make([]byte, 0)
|
||||||
if _, err := f.Read(buf); err != nil {
|
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
|
return []byte{crc8.Checksum(buf, hasherSelect(crcTable[0]))}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ package md5
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
@ -24,13 +25,13 @@ func MD5Hex(data []byte) string {
|
||||||
func SumFile(file string) ([]byte, error) {
|
func SumFile(file string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
hasher := md5.New()
|
hasher := md5.New()
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ package phash
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -27,13 +28,13 @@ func HashHex(image image.Image) string {
|
||||||
func HashFile(file string) ([]byte, error) {
|
func HashFile(file string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
img, _, err := image.Decode(f)
|
img, _, err := image.Decode(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能解码指定图像文件,%w", err)
|
||||||
}
|
}
|
||||||
return Hash(img), nil
|
return Hash(img), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ package sha1
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
@ -24,13 +25,13 @@ func Sha1Hex(data []byte) string {
|
||||||
func SumFile(file string) ([]byte, error) {
|
func SumFile(file string) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
hasher := sha1.New()
|
hasher := sha1.New()
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ package sha256
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -60,13 +61,13 @@ func Sum256Hex(data []byte, bitSize ...int) string {
|
||||||
func SumFile256(file string, bitSize ...int) ([]byte, error) {
|
func SumFile256(file string, bitSize ...int) ([]byte, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
hasher := hasherSelect(bitSize[0])
|
hasher := hasherSelect(bitSize[0])
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ package sha512
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -76,14 +77,14 @@ func Sum512Hex(data []byte, bitSize ...int) string {
|
||||||
func SumFile512(path string, bitSize ...int) ([]byte, error) {
|
func SumFile512(path string, bitSize ...int) ([]byte, error) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能打开指定文件,%w", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
length := append(bitSize, 512)
|
length := append(bitSize, 512)
|
||||||
var hasher = hasherSelect(length[0])
|
var hasher = hasherSelect(length[0])
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("未能读取指定文件的内容,%w", err)
|
||||||
}
|
}
|
||||||
return hasher.Sum(nil), nil
|
return hasher.Sum(nil), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user