ag_tools/cmd/crc32.go

51 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"strings"
"archgrid.xyz/ag/tools/types"
"archgrid.xyz/ag/toolsbox/hash/crc32"
"github.com/spf13/cobra"
)
var (
crc32Algorithm types.CRC32Algorithm = types.CRC32_IEEE
crc32File bool
)
var crc32Cmd = &cobra.Command{
Use: "crc32",
Short: `CRC32校验`,
Long: `对给定的内容进行CRC32校验计算。`,
Args: cobra.MinimumNArgs(1),
Run: crc32Execute,
}
func crc32Execute(cmd *cobra.Command, args []string) {
var (
result []byte
err error
)
if crc32File {
result, err = crc32.SumFile(args[0], crc32Algorithm.IntoCRC32Mode())
} else {
result = crc32.CRC32([]byte(args[0]), crc32Algorithm.IntoCRC32Mode())
}
if err != nil {
fmt.Printf("计算CRC32校验值时发生错误%s\n", err)
return
}
var builder strings.Builder
for _, b := range result {
builder.WriteString(fmt.Sprintf(" %02X", b))
}
fmt.Printf("CRC32校验值%s\n", builder.String())
}
func init() {
crc32Cmd.PersistentFlags().VarP(&crc32Algorithm, "algorithm", "a", "CRC32算法可选值有ieee, castagnoli, koopman缺省值为ieee。")
crc32Cmd.PersistentFlags().BoolVarP(&crc32File, "file", "f", false, "从文件中读取内容")
rootCmd.AddCommand(crc32Cmd)
}