You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/hash/hash.go

269 lines
7.3 KiB
Go

2 years ago
package hash
import (
9 months ago
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
2 years ago
"encoding/hex"
"fmt"
9 months ago
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
"hash"
"hash/crc32"
"io"
2 years ago
"os"
"b612.me/starlog"
"github.com/spf13/cobra"
)
9 months ago
var hmacKey string
2 years ago
var Cmd = &cobra.Command{
Use: "hash",
Short: "多种方法哈希值校验",
Long: "进行多种方法哈希值校验",
Run: func(this *cobra.Command, args []string) {
var cumethod, method []string
var result = make(map[string]string)
var err error
9 months ago
cumethod = []string{"md5", "crc32", "sha512", "sha384", "sha256", "sha224", "sha1", "md4", "ripemd160", "hmacmd5", "hmacmd4", "hmacsha1", "hmacsha224", "hmacsha256", "hmacsha384", "hmacsha512"}
2 years ago
if ok, _ := this.Flags().GetBool("all"); ok {
method = cumethod
} else {
if len(args) == 0 {
this.Usage()
os.Exit(1)
}
for _, v := range cumethod {
if ok, _ := this.Flags().GetBool(v); ok {
method = append(method, v)
}
}
if len(method) == 0 {
method = append(method, "md5")
}
}
if ok, _ := this.Flags().GetBool("file"); ok {
9 months ago
result, err = FileSumAll(args[0], hmacKey, method, func(pect float64) {
2 years ago
if pect != 100.0 {
fmt.Printf("校验已完成:%f%%\r", pect)
} else {
fmt.Printf("校验已完成:%f%%\n", pect)
}
})
} else {
var bres map[string][]byte
9 months ago
bres, err = SumAll([]byte(args[0]), hmacKey, method)
2 years ago
if err == nil {
for k, v := range bres {
result[k] = hex.EncodeToString(v)
}
}
}
if err != nil {
starlog.Criticalln("错误:" + err.Error())
}
for _, v := range method {
fmt.Printf("%s%s\n", v, result[v])
}
},
}
func init() {
9 months ago
Cmd.Flags().StringVarP(&hmacKey, "hmac-key", "k", "b612.me", "HMAC密钥")
2 years ago
Cmd.Flags().BoolP("all", "a", false, "使用所有的校验方法")
Cmd.Flags().BoolP("file", "f", false, "对指定文件进行校验")
Cmd.Flags().BoolP("md5", "m", false, "进行MD5校验默认")
Cmd.Flags().BoolP("crc32", "c", false, "进行CRC32校验")
Cmd.Flags().BoolP("sha512", "s", false, "进行SHA512校验")
Cmd.Flags().Bool("sha384", false, "进行SHA384校验")
Cmd.Flags().Bool("sha256", false, "进行SHA256校验")
Cmd.Flags().Bool("sha224", false, "进行SHA224校验")
Cmd.Flags().Bool("sha1", false, "进行SHA1校验")
9 months ago
Cmd.Flags().Bool("md4", false, "进行MD4校验")
Cmd.Flags().Bool("ripemd160", false, "进行RIPEMD160校验")
Cmd.Flags().Bool("hmacmd5", false, "进行HMACMD5校验")
Cmd.Flags().Bool("hmacmd4", false, "进行HMACMD4校验")
Cmd.Flags().Bool("hmacsha1", false, "进行HMACSHA1校验")
Cmd.Flags().Bool("hmacsha224", false, "进行HMACSHA224校验")
Cmd.Flags().Bool("hmacsha256", false, "进行HMACSHA256校验")
Cmd.Flags().Bool("hmacsha384", false, "进行HMACSHA384校验")
Cmd.Flags().Bool("hmacsha512", false, "进行HMACSHA512校验")
}
func FileSumAll(filepath string, key string, method []string, shell func(float64)) (map[string]string, error) {
result := make(map[string]string)
methods := make(map[string]hash.Hash)
var iscrc bool
if len(method) == 0 {
method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5", "md4", "ripemd160", "hmacmd5", "hmacmd4", "hmacsha1", "hmacsha224", "hmacsha256", "hmacsha384", "hmacsha512"}
}
fp, err := os.Open(filepath)
defer fp.Close()
if err != nil {
return result, err
}
stat, _ := os.Stat(filepath)
filebig := float64(stat.Size())
sum512 := sha512.New()
sum384 := sha512.New384()
sum256 := sha256.New()
sum224 := sha256.New224()
sum1 := sha1.New()
crcsum := crc32.NewIEEE()
md5sum := md5.New()
md4sum := md4.New()
ripemd160sum := ripemd160.New()
hmacmd5sum := hmac.New(md5.New, []byte(key))
hmacmd4sum := hmac.New(md4.New, []byte(key))
hmacsha1sum := hmac.New(sha1.New, []byte(key))
hmacsha224sum := hmac.New(sha256.New224, []byte(key))
hmacsha256sum := hmac.New(sha256.New, []byte(key))
hmacsha384sum := hmac.New(sha512.New384, []byte(key))
hmacsha512sum := hmac.New(sha512.New, []byte(key))
for _, v := range method {
switch v {
case "md5":
methods["md5"] = md5sum
case "crc32":
iscrc = true
case "sha1":
methods["sha1"] = sum1
case "sha224":
methods["sha224"] = sum224
case "sha256":
methods["sha256"] = sum256
case "sha384":
methods["sha384"] = sum384
case "sha512":
methods["sha512"] = sum512
case "md4":
methods["md4"] = md4sum
case "ripemd160":
methods["ripemd160"] = ripemd160sum
case "hmacmd5":
methods["hmacmd5"] = hmacmd5sum
case "hmacmd4":
methods["hmacmd4"] = hmacmd4sum
case "hmacsha1":
methods["hmacsha1"] = hmacsha1sum
case "hmacsha224":
methods["hmacsha224"] = hmacsha224sum
case "hmacsha256":
methods["hmacsha256"] = hmacsha256sum
case "hmacsha384":
methods["hmacsha384"] = hmacsha384sum
case "hmacsha512":
methods["hmacsha512"] = hmacsha512sum
}
}
writer := 0
for {
buf := make([]byte, 1048574)
n, err := fp.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return result, err
}
writer += n
pect := (float64(writer) / filebig) * 100
go shell(pect)
for _, v := range methods {
v.Write(buf[0:n])
}
if iscrc {
crcsum.Write(buf[0:n])
}
}
for k, v := range methods {
result[k] = hex.EncodeToString(v.Sum(nil))
}
if iscrc {
result["crc32"] = hex.EncodeToString(crcsum.Sum(nil))
}
return result, nil
}
func SumAll(data []byte, key string, method []string) (map[string][]byte, error) {
result := make(map[string][]byte)
methods := make(map[string]hash.Hash)
var iscrc bool
if len(method) == 0 {
method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5", "md4", "ripemd160", "hmacmd5", "hmacmd4", "hmacsha1", "hmacsha224", "hmacsha256", "hmacsha384", "hmacsha512"}
}
sum512 := sha512.New()
sum384 := sha512.New384()
sum256 := sha256.New()
sum224 := sha256.New224()
sum1 := sha1.New()
crcsum := crc32.NewIEEE()
md5sum := md5.New()
md4sum := md4.New()
ripemd160sum := ripemd160.New()
hmacmd5sum := hmac.New(md5.New, []byte(key))
hmacmd4sum := hmac.New(md4.New, []byte(key))
hmacsha1sum := hmac.New(sha1.New, []byte(key))
hmacsha224sum := hmac.New(sha256.New224, []byte(key))
hmacsha256sum := hmac.New(sha256.New, []byte(key))
hmacsha384sum := hmac.New(sha512.New384, []byte(key))
hmacsha512sum := hmac.New(sha512.New, []byte(key))
for _, v := range method {
switch v {
case "md5":
methods["md5"] = md5sum
case "crc32":
iscrc = true
case "sha1":
methods["sha1"] = sum1
case "sha224":
methods["sha224"] = sum224
case "sha256":
methods["sha256"] = sum256
case "sha384":
methods["sha384"] = sum384
case "sha512":
methods["sha512"] = sum512
case "md4":
methods["md4"] = md4sum
case "ripemd160":
methods["ripemd160"] = ripemd160sum
case "hmacmd5":
methods["hmacmd5"] = hmacmd5sum
case "hmacmd4":
methods["hmacmd4"] = hmacmd4sum
case "hmacsha1":
methods["hmacsha1"] = hmacsha1sum
case "hmacsha224":
methods["hmacsha224"] = hmacsha224sum
case "hmacsha256":
methods["hmacsha256"] = hmacsha256sum
case "hmacsha384":
methods["hmacsha384"] = hmacsha384sum
case "hmacsha512":
methods["hmacsha512"] = hmacsha512sum
}
}
for _, v := range methods {
v.Write(data)
}
if iscrc {
crcsum.Write(data)
}
for k, v := range methods {
result[k] = v.Sum(nil)
}
if iscrc {
result["crc32"] = crcsum.Sum(nil)
}
return result, nil
2 years ago
}