package hash import ( "b612.me/starcrypto" "encoding/hex" "fmt" "os" "b612.me/starlog" "github.com/spf13/cobra" ) 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 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 = starcrypto.FileSumAll(args[0], method, func(pect float64) { if pect != 100.0 { fmt.Printf("校验已完成:%f%%\r", pect) } else { fmt.Printf("校验已完成:%f%%\n", pect) } }) } else { var bres map[string][]byte bres, err = starcrypto.SumAll([]byte(args[0]), method) 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() { 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校验") }