|
|
package main
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"os"
|
|
|
|
|
|
"b612.me/starainrt"
|
|
|
|
|
|
"b612.me/starlog"
|
|
|
"github.com/spf13/cobra"
|
|
|
)
|
|
|
|
|
|
var hashcmd = &cobra.Command{
|
|
|
Use: "hash",
|
|
|
Short: "多种方法哈希值校验",
|
|
|
Long: "进行多种方法哈希值校验",
|
|
|
Run: func(this *cobra.Command, args []string) {
|
|
|
var cumethod, method []string
|
|
|
var result map[string]string
|
|
|
var err error
|
|
|
crypto := new(starainrt.StarCrypto)
|
|
|
cumethod = []string{"md5", "crc32", "sha512", "sha384", "sha256", "sha224", "sha1"}
|
|
|
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 {
|
|
|
result, err = crypto.FileSumAll(args[0], method, func(pect float64) {
|
|
|
if pect != 100.0 {
|
|
|
fmt.Printf("校验已完成:%f%%\r", pect)
|
|
|
} else {
|
|
|
fmt.Printf("校验已完成:%f%%\n", pect)
|
|
|
}
|
|
|
})
|
|
|
} else {
|
|
|
result, err = crypto.SumAll([]byte(args[0]), method)
|
|
|
}
|
|
|
if err != nil {
|
|
|
starlog.Criticalln("错误:" + err.Error())
|
|
|
}
|
|
|
for _, v := range method {
|
|
|
fmt.Printf("%s:%s\n", v, result[v])
|
|
|
}
|
|
|
},
|
|
|
}
|
|
|
|
|
|
func init() {
|
|
|
hashcmd.Flags().BoolP("all", "a", false, "使用所有的校验方法")
|
|
|
hashcmd.Flags().BoolP("file", "f", false, "对指定文件进行校验")
|
|
|
hashcmd.Flags().BoolP("md5", "m", false, "进行MD5校验(默认)")
|
|
|
hashcmd.Flags().BoolP("crc32", "c", false, "进行CRC32校验")
|
|
|
hashcmd.Flags().BoolP("sha512", "s", false, "进行SHA512校验")
|
|
|
hashcmd.Flags().Bool("sha384", false, "进行SHA384校验")
|
|
|
hashcmd.Flags().Bool("sha256", false, "进行SHA256校验")
|
|
|
hashcmd.Flags().Bool("sha224", false, "进行SHA224校验")
|
|
|
hashcmd.Flags().Bool("sha1", false, "进行SHA1校验")
|
|
|
}
|