build(init):项目加入版本库控制。
This commit is contained in:
113
cmd/aes.go
Normal file
113
cmd/aes.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/aes"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
aesKey string
|
||||
aesResult types.ResultEncoding = types.ResultInHex
|
||||
aesPadding types.EncryptionPadding = types.PKCS7Padding
|
||||
)
|
||||
|
||||
var aesCommand = &cobra.Command{
|
||||
Use: "aes",
|
||||
Short: "AES加密算法工具",
|
||||
Long: `使用AES加密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
|
||||
}
|
||||
|
||||
var aesEncryptCmd = &cobra.Command{
|
||||
Use: "encrypt",
|
||||
Short: "使用AES加密算法加密",
|
||||
Long: "使用AES加密算法加密,支持三种填充方式:PKCS7、Zero和无填充。默认输出十六进制编码字符串。",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: aesEncryptExecute,
|
||||
}
|
||||
|
||||
func aesEncryptExecute(cmd *cobra.Command, args []string) {
|
||||
var pad encryption.PaddingMode
|
||||
switch aesPadding {
|
||||
case types.PKCS7Padding:
|
||||
pad = encryption.PKCS7Padding
|
||||
case types.ZeroPadding:
|
||||
pad = encryption.ZeroPadding
|
||||
case types.NoPadding:
|
||||
pad = encryption.NoPadding
|
||||
default:
|
||||
fmt.Printf("不支持的填充方式:%s\n", aesPadding)
|
||||
return
|
||||
}
|
||||
encodedResult, err := aes.Encrypt([]byte(args[0]), []byte(aesKey), pad)
|
||||
if err != nil {
|
||||
fmt.Printf("加密失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch aesResult {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("加密结果:%s\n", base64.ToBase64(encodedResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("加密结果:%s\n", hex.ToHex(encodedResult))
|
||||
}
|
||||
}
|
||||
|
||||
var aesDecryptCmd = &cobra.Command{
|
||||
Use: "decrypt",
|
||||
Short: "使用AES加密算法解密",
|
||||
Long: "使用AES加密算法解密,支持三种填充方式:PKCS7、Zero和无填充。默认输入十六进制编码字符串。",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: aesDecryptExecute,
|
||||
}
|
||||
|
||||
func aesDecryptExecute(cmd *cobra.Command, args []string) {
|
||||
var pad encryption.PaddingMode
|
||||
switch aesPadding {
|
||||
case types.PKCS7Padding:
|
||||
pad = encryption.PKCS7Padding
|
||||
case types.ZeroPadding:
|
||||
pad = encryption.ZeroPadding
|
||||
case types.NoPadding:
|
||||
pad = encryption.NoPadding
|
||||
default:
|
||||
fmt.Printf("不支持的填充方式:%s\n", aesPadding)
|
||||
return
|
||||
}
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
)
|
||||
switch aesResult {
|
||||
case types.ResultInBase64:
|
||||
data, err = base64.FromBase64(args[0])
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
data, err = hex.FromHex(args[0])
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败,提供的密文无法解析,%s\n", err)
|
||||
return
|
||||
}
|
||||
decodeResult, err := aes.Decrypt(data, []byte(aesKey), pad)
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("解密结果:%s\n", decodeResult)
|
||||
}
|
||||
|
||||
func init() {
|
||||
aesCommand.PersistentFlags().StringVarP(&aesKey, "key", "k", "", "密钥")
|
||||
aesCommand.PersistentFlags().VarP(&aesResult, "output", "o", "输入或输出使用Base64编码或者Hex编码,默认为Hex编码。")
|
||||
aesCommand.PersistentFlags().VarP(&aesPadding, "padding", "p", "填充方式,可选值:pkcs7、zero、none,默认使用pkcs7。")
|
||||
aesCommand.AddCommand(aesEncryptCmd, aesDecryptCmd)
|
||||
rootCmd.AddCommand(aesCommand)
|
||||
}
|
45
cmd/base64.go
Normal file
45
cmd/base64.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var base64Cmd = &cobra.Command{
|
||||
Use: "base64",
|
||||
Short: "对给定内容进行base64编码或解码",
|
||||
}
|
||||
|
||||
var base64EncodeCmd = &cobra.Command{
|
||||
Use: "encode",
|
||||
Short: "对给定内容进行base64编码",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: base64EncodeExecute,
|
||||
}
|
||||
|
||||
func base64EncodeExecute(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("Base64编码结果:%s\n", base64.ToBase64([]byte(args[0])))
|
||||
}
|
||||
|
||||
var base64DecodeCmd = &cobra.Command{
|
||||
Use: "decode",
|
||||
Short: "对给定内容进行base64解码",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: base64DecodeExecute,
|
||||
}
|
||||
|
||||
func base64DecodeExecute(cmd *cobra.Command, args []string) {
|
||||
decoded, err := base64.FromBase64(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("Base64解码失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Base64解码结果:%s\n", decoded)
|
||||
}
|
||||
|
||||
func init() {
|
||||
base64Cmd.AddCommand(base64EncodeCmd, base64DecodeCmd)
|
||||
rootCmd.AddCommand(base64Cmd)
|
||||
}
|
50
cmd/crc16.go
Normal file
50
cmd/crc16.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/crc16"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
crc16Algorithm types.CRC16Algorithm = types.CRC16_IBM
|
||||
crc16File bool
|
||||
)
|
||||
|
||||
var crc16Cmd = &cobra.Command{
|
||||
Use: "crc16",
|
||||
Short: `CRC16校验`,
|
||||
Long: `对给定的内容进行CRC16校验计算。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: crc16Execute,
|
||||
}
|
||||
|
||||
func crc16Execute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
result []byte
|
||||
err error
|
||||
)
|
||||
if crc16File {
|
||||
result, err = crc16.SumFile(args[0], crc16Algorithm.IntoCRC16Mode())
|
||||
} else {
|
||||
result = crc16.CRC16([]byte(args[0]), crc16Algorithm.IntoCRC16Mode())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("计算CRC16校验值时发生错误:%s\n", err)
|
||||
return
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, b := range result {
|
||||
builder.WriteString(fmt.Sprintf(" %02X", b))
|
||||
}
|
||||
fmt.Printf("CRC16校验值:%s\n", builder.String())
|
||||
}
|
||||
|
||||
func init() {
|
||||
crc16Cmd.PersistentFlags().VarP(&crc16Algorithm, "algorithm", "a", "CRC16算法,可选值有:ccitt, ccitt-false, scsi, ibm, mbus,缺省值为ibm。")
|
||||
crc16Cmd.PersistentFlags().BoolVarP(&crc16File, "file", "f", false, "从文件中读取内容")
|
||||
rootCmd.AddCommand(crc16Cmd)
|
||||
}
|
50
cmd/crc32.go
Normal file
50
cmd/crc32.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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)
|
||||
}
|
50
cmd/crc64.go
Normal file
50
cmd/crc64.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/crc64"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
crc64Algorithm types.CRC64Algorithm = types.CRC64_ISO
|
||||
crc64File bool
|
||||
)
|
||||
|
||||
var crc64Cmd = &cobra.Command{
|
||||
Use: "crc64",
|
||||
Short: `CRC64校验`,
|
||||
Long: `对给定的内容进行CRC64校验计算。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: crc64Execute,
|
||||
}
|
||||
|
||||
func crc64Execute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
result []byte
|
||||
err error
|
||||
)
|
||||
if crc64File {
|
||||
result, err = crc64.SumFile(args[0], crc64Algorithm.IntoCRC64Mode())
|
||||
} else {
|
||||
result = crc64.CRC64([]byte(args[0]), crc64Algorithm.IntoCRC64Mode())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("计算CRC64校验值时发生错误:%s\n", err)
|
||||
return
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, b := range result {
|
||||
builder.WriteString(fmt.Sprintf(" %02X", b))
|
||||
}
|
||||
fmt.Printf("CRC64校验值:%s\n", builder.String())
|
||||
}
|
||||
|
||||
func init() {
|
||||
crc64Cmd.PersistentFlags().VarP(&crc64Algorithm, "algorithm", "a", "CRC64算法,可选值有:iso, ecma,缺省值为iso。")
|
||||
crc64Cmd.PersistentFlags().BoolVarP(&crc64File, "file", "f", false, "从文件中读取内容")
|
||||
rootCmd.AddCommand(crc64Cmd)
|
||||
}
|
50
cmd/crc8.go
Normal file
50
cmd/crc8.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/crc8"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
crc8Algorithm types.CRC8Algorithm
|
||||
crc8File bool
|
||||
)
|
||||
|
||||
var crc8Cmd = &cobra.Command{
|
||||
Use: "crc8",
|
||||
Short: `CRC8校验`,
|
||||
Long: `对给定的内容进行CRC8校验计算。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: crc8Execute,
|
||||
}
|
||||
|
||||
func crc8Execute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
result []byte
|
||||
err error
|
||||
)
|
||||
if crc8File {
|
||||
result, err = crc8.SumFile(args[0], crc8Algorithm.IntoCRC8Mode())
|
||||
} else {
|
||||
result = crc8.CRC8([]byte(args[0]), crc8Algorithm.IntoCRC8Mode())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("计算CRC8校验值时发生错误:%s\n", err)
|
||||
return
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, b := range result {
|
||||
builder.WriteString(fmt.Sprintf(" %02X", b))
|
||||
}
|
||||
fmt.Printf("CRC8校验值:%s\n", builder.String())
|
||||
}
|
||||
|
||||
func init() {
|
||||
crc8Cmd.PersistentFlags().VarP(&crc8Algorithm, "algorithm", "a", "CRC8算法,可选值有:crc8, cdma2000, darc, dvb-s2, ebu, i-code, itu, maxim, rohc, wcdma")
|
||||
crc8Cmd.PersistentFlags().BoolVarP(&crc8File, "file", "f", false, "从文件中读取内容")
|
||||
rootCmd.AddCommand(crc8Cmd)
|
||||
}
|
113
cmd/des.go
Normal file
113
cmd/des.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/des"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
desKey string
|
||||
desResult types.ResultEncoding = types.ResultInHex
|
||||
desPadding types.EncryptionPadding = types.PKCS7Padding
|
||||
)
|
||||
|
||||
var desCommand = &cobra.Command{
|
||||
Use: "des",
|
||||
Short: "DES加密算法工具",
|
||||
Long: `使用DES加密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
|
||||
}
|
||||
|
||||
var desEncryptCmd = &cobra.Command{
|
||||
Use: "encrypt",
|
||||
Short: "使用DES加密算法加密",
|
||||
Long: `使用DES加密算法加密,支持三种填充方式:PKCS7、Zero和无填充。默认输出十六进制编码字符串。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: desEncryptExecute,
|
||||
}
|
||||
|
||||
var desDecryptCmd = &cobra.Command{
|
||||
Use: "decrypt",
|
||||
Short: "使用DES加密算法解密",
|
||||
Long: `使用DES加密算法解密,支持三种填充方式:PKCS7、Zero和无填充。默认输入十六进制编码字符串。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: desDecryptExecute,
|
||||
}
|
||||
|
||||
func desEncryptExecute(cmd *cobra.Command, args []string) {
|
||||
var pad encryption.PaddingMode
|
||||
switch desPadding {
|
||||
case types.PKCS7Padding:
|
||||
pad = encryption.PKCS7Padding
|
||||
case types.ZeroPadding:
|
||||
pad = encryption.ZeroPadding
|
||||
case types.NoPadding:
|
||||
pad = encryption.NoPadding
|
||||
default:
|
||||
fmt.Printf("不支持的填充方式:%s\n", desPadding)
|
||||
return
|
||||
}
|
||||
encodedResult, err := des.Encrypt([]byte(args[0]), []byte(desKey), pad)
|
||||
if err != nil {
|
||||
fmt.Printf("加密失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch desResult {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("加密结果:%s\n", base64.ToBase64URLEncoded(encodedResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("加密结果:%s\n", hex.ToHex(encodedResult))
|
||||
}
|
||||
}
|
||||
|
||||
func desDecryptExecute(cmd *cobra.Command, args []string) {
|
||||
var pad encryption.PaddingMode
|
||||
switch desPadding {
|
||||
case types.PKCS7Padding:
|
||||
pad = encryption.PKCS7Padding
|
||||
case types.ZeroPadding:
|
||||
pad = encryption.ZeroPadding
|
||||
case types.NoPadding:
|
||||
pad = encryption.NoPadding
|
||||
default:
|
||||
fmt.Printf("不支持的填充方式:%s\n", desPadding)
|
||||
return
|
||||
}
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
)
|
||||
switch desResult {
|
||||
case types.ResultInBase64:
|
||||
data, err = base64.FromBase64(args[0])
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
data, err = hex.FromHex(args[0])
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败,提供的密文无法解析,%s\n", err)
|
||||
return
|
||||
}
|
||||
decodedResult, err := des.Decrypt(data, []byte(desKey), pad)
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败,%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("解密结果:%s\n", decodedResult)
|
||||
}
|
||||
|
||||
func init() {
|
||||
desCommand.PersistentFlags().VarP(&desResult, "output", "o", "密文的编码方式,可选Base64或者Hex,同时对加密和解密过程有效,默认使用Hex编码。")
|
||||
desCommand.PersistentFlags().StringVarP(&desKey, "key", "k", "", "密钥。")
|
||||
desCommand.PersistentFlags().VarP(&desPadding, "padding", "p", "填充方式,支持pkcs7、zero和none,默认为pkcs7。")
|
||||
desCommand.AddCommand(desEncryptCmd, desDecryptCmd)
|
||||
rootCmd.AddCommand(desCommand)
|
||||
}
|
39
cmd/hail.go
Normal file
39
cmd/hail.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/toolsbox/serial_code/hail"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
hailHostSerial int64
|
||||
hailPrefix string
|
||||
)
|
||||
|
||||
var hailCmd = &cobra.Command{
|
||||
Use: "hail",
|
||||
Short: "使用冰雹ID生成算法生成一个唯一ID",
|
||||
Run: hailGenerateExecute,
|
||||
}
|
||||
|
||||
func hailGenerateExecute(cmd *cobra.Command, args []string) {
|
||||
hail.Initialize(hailHostSerial)
|
||||
engine, err := hail.Get()
|
||||
if err != nil {
|
||||
fmt.Printf("未能初始化冰雹ID生成器: %s\n", err)
|
||||
return
|
||||
}
|
||||
if len(hailPrefix) > 0 {
|
||||
fmt.Printf("生成的ID:%s\n", engine.GeneratePrefixedString(hailPrefix))
|
||||
} else {
|
||||
fmt.Printf("生成的ID:%s\n", engine.GenerateString())
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
hailCmd.Flags().Int64VarP(&hailHostSerial, "host-serial", "s", 1, "设定冰雹ID生成器的主机编号,默认为1。")
|
||||
hailCmd.Flags().StringVarP(&hailPrefix, "prefix", "p", "", "设定生成的ID的前缀内容,默认为空。")
|
||||
rootCmd.AddCommand(hailCmd)
|
||||
}
|
54
cmd/md5.go
Normal file
54
cmd/md5.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/md5"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
md5File bool
|
||||
md5Result types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var md5Cmd = &cobra.Command{
|
||||
Use: "md5",
|
||||
Short: "使用md5算法计算文件或者字符串的哈希值。",
|
||||
Long: `使用md5算法计算文件或者字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: md5HashExecute,
|
||||
}
|
||||
|
||||
func md5HashExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
hashResult []byte
|
||||
err error
|
||||
)
|
||||
if md5File {
|
||||
hashResult, err = md5.SumFile(args[0])
|
||||
} else {
|
||||
hashResult = md5.MD5([]byte(args[0]))
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("散列计算失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch md5Result {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
md5Cmd.PersistentFlags().BoolVarP(&md5File, "file", "f", false, "使用文件模式。")
|
||||
md5Cmd.PersistentFlags().VarP(&md5Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
|
||||
rootCmd.AddCommand(md5Cmd)
|
||||
}
|
48
cmd/phash.go
Normal file
48
cmd/phash.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/phash"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
phashResult types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var phashCmd = &cobra.Command{
|
||||
Use: "phash",
|
||||
Short: "使用pHash算法计算指定图像文件的感知哈希值。",
|
||||
Long: `使用pHash算法计算指定图像文件的感知哈希值。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: phashExecute,
|
||||
}
|
||||
|
||||
func phashExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
hashResult []byte
|
||||
err error
|
||||
)
|
||||
hashResult, err = phash.HashFile(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("散列计算失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch phashResult {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("感知计算结果:%s\n", base64.ToBase64(hashResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("感知计算结果:%s\n", hex.ToHex(hashResult))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
phashCmd.Flags().VarP(&phashResult, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
|
||||
rootCmd.AddCommand(phashCmd)
|
||||
}
|
35
cmd/rootCmd.go
Normal file
35
cmd/rootCmd.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"archgrid.xyz/ag/tools/cmd/rsa"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "agtools",
|
||||
Short: "AGTools是一个基于ag_toolsbox开发的工具集合",
|
||||
Long: `AGTools是一个基于ag_toolsbox开发的工具集合,主要提供了常用加密、散列、时间生成、随机字符串生成等方法。`,
|
||||
}
|
||||
|
||||
var versionCommand = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "输出当前工具版本号",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("AGTools v0.0.1")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCommand)
|
||||
rootCmd.AddCommand(rsa.GetRsaCmd())
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Printf("无法执行命令: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
126
cmd/rsa/decryption.go
Normal file
126
cmd/rsa/decryption.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package rsa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/rsa"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rsaDecryptionFromFile bool
|
||||
rsaDecryptionToFile bool
|
||||
rsaDecryptionFromEncoding types.ResultEncoding = types.ResultInHex
|
||||
rsaDecryptionInputFile string
|
||||
rsaDecryptionOutputFile string
|
||||
)
|
||||
|
||||
var rsaDecryptionCmd = &cobra.Command{
|
||||
Use: "decrypt",
|
||||
Short: `使用RSA算法对给定的内容进行解密`,
|
||||
Long: `使用RSA算法对给定的内容进行解密,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行解密,否则从命令行读取内容进行解密。如果指定了输出文件,则将解密后的内容写入到输出文件中,否则将解密后的内容输出到命令行。`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: rsaDecryptionExecute,
|
||||
}
|
||||
|
||||
func rsaDecryptionExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
content []byte
|
||||
cert []byte
|
||||
keys *rsa.KeyPair
|
||||
err error
|
||||
)
|
||||
if len(privateKeyFile) > 0 {
|
||||
file, err := os.Open(privateKeyFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
cert, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Println("必须提供RSA私钥文件才能够解密。")
|
||||
return
|
||||
}
|
||||
keys, err = rsa.NewFromPEM(cert)
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析RSA私钥:%s\n", err)
|
||||
return
|
||||
}
|
||||
if rsaDecryptionFromFile {
|
||||
if len(rsaDecryptionInputFile) == 0 {
|
||||
fmt.Println("必须指定输入文件名。")
|
||||
return
|
||||
}
|
||||
file, err := os.Open(rsaDecryptionInputFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
content, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if len(args) == 0 {
|
||||
fmt.Println("必须指定待解密的内容。")
|
||||
return
|
||||
}
|
||||
switch rsaDecryptionFromEncoding {
|
||||
case types.ResultInHex:
|
||||
content, err = hex.FromHex(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析十六进制编码:%s\n", err)
|
||||
return
|
||||
}
|
||||
case types.ResultInBase64:
|
||||
content, err = base64.FromBase64(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析Base64编码:%s\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
decrypted, err := keys.Decrypt(content)
|
||||
if err != nil {
|
||||
fmt.Printf("无法解密:%s\n", err)
|
||||
return
|
||||
}
|
||||
if rsaDecryptionToFile {
|
||||
if len(rsaDecryptionOutputFile) == 0 {
|
||||
fmt.Println("必须指定输出文件名。")
|
||||
return
|
||||
}
|
||||
file, err := os.OpenFile(rsaDecryptionOutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开输出文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = file.Write(decrypted)
|
||||
if err != nil {
|
||||
fmt.Printf("无法写入输出文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("解密结果:%s\n", decrypted)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rsaDecryptionCmd.Flags().BoolVarP(&rsaDecryptionFromFile, "from-file", "f", false, "从文件中读取待解密的内容")
|
||||
rsaDecryptionCmd.Flags().BoolVarP(&rsaDecryptionToFile, "to-file", "t", false, "将解密后的内容写入到文件中")
|
||||
rsaDecryptionCmd.Flags().VarP(&rsaDecryptionFromEncoding, "from-encoding", "e", "指定输出格式,可选值为hex、base64,默认值为hex。对文件输入格式不起作用。")
|
||||
rsaDecryptionCmd.Flags().StringVarP(&rsaDecryptionInputFile, "input-file", "i", "", "指定输入文件名")
|
||||
rsaDecryptionCmd.Flags().StringVarP(&rsaDecryptionOutputFile, "output-file", "o", "", "指定输出文件名")
|
||||
rsaCmd.AddCommand(rsaDecryptionCmd)
|
||||
}
|
127
cmd/rsa/encryption.go
Normal file
127
cmd/rsa/encryption.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package rsa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/rsa"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rsaEncryptFromFile bool
|
||||
rsaEncryptToFile bool
|
||||
rsaResultOutput types.ResultEncoding = types.ResultInHex
|
||||
rsaEncryptInputFile string
|
||||
rsaEncryptOutputFile string
|
||||
)
|
||||
|
||||
var rsaEncryptCmd = &cobra.Command{
|
||||
Use: "encrypt",
|
||||
Short: `使用RSA算法对给定的内容进行加密`,
|
||||
Long: `使用RSA算法对给定的内容进行加密,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行加密,否则从命令行读取内容进行加密。如果指定了输出文件,则将加密后的内容写入到输出文件中,否则将加密后的内容输出到命令行。`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: rsaEncryptExecute,
|
||||
}
|
||||
|
||||
func rsaEncryptExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
content []byte
|
||||
cert []byte
|
||||
keys *rsa.KeyPair
|
||||
err error
|
||||
)
|
||||
if len(publicKeyFile) > 0 {
|
||||
file, err := os.Open(publicKeyFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开RSA公钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
cert, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取RSA公钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else if len(privateKeyFile) > 0 {
|
||||
file, err := os.Open(privateKeyFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
cert, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Println("必须指定RSA公钥文件或RSA私钥文件。")
|
||||
return
|
||||
}
|
||||
keys, err = rsa.NewFromPEM(cert)
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析RSA密钥:%s\n", err)
|
||||
return
|
||||
}
|
||||
if rsaEncryptFromFile {
|
||||
if len(rsaEncryptInputFile) == 0 {
|
||||
fmt.Println("必须指定输入文件。")
|
||||
return
|
||||
}
|
||||
file, err := os.Open(rsaEncryptInputFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
content, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if len(args) == 0 {
|
||||
fmt.Println("必须指定要加密的内容。")
|
||||
return
|
||||
}
|
||||
content = []byte(args[0])
|
||||
}
|
||||
encrypted, err := keys.Encrypt(content)
|
||||
if err != nil {
|
||||
fmt.Printf("无法加密内容:%s\n", err)
|
||||
return
|
||||
}
|
||||
if len(rsaEncryptOutputFile) > 0 {
|
||||
file, err := os.OpenFile(rsaEncryptOutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
fmt.Printf("无法创建RSA加密文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
_, err = file.Write(encrypted)
|
||||
if err != nil {
|
||||
fmt.Printf("无法写入RSA加密文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("已将加密结果写入到文件:%s\n", rsaEncryptOutputFile)
|
||||
} else {
|
||||
switch rsaResultOutput {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("加密结果:%s\n", base64.ToBase64(encrypted))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("加密结果:%s\n", hex.ToHex(encrypted))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rsaEncryptCmd.PersistentFlags().BoolVarP(&rsaEncryptFromFile, "from-file", "f", false, "从输入文件中读取内容进行加密。")
|
||||
rsaEncryptCmd.PersistentFlags().BoolVarP(&rsaEncryptToFile, "to-file", "t", false, "将加密后的内容写入到输出文件中。")
|
||||
rsaEncryptCmd.PersistentFlags().StringVarP(&rsaEncryptInputFile, "input-file", "i", "", "输入文件。")
|
||||
rsaEncryptCmd.PersistentFlags().StringVarP(&rsaEncryptOutputFile, "output-file", "o", "", "输出文件。")
|
||||
rsaEncryptCmd.PersistentFlags().VarP(&rsaResultOutput, "output-encode", "e", "加密结果输出格式,可选值有:hex,base64,缺省值为hex。对输出到文件的方式无效。")
|
||||
rsaCmd.AddCommand(rsaEncryptCmd)
|
||||
}
|
71
cmd/rsa/key.go
Normal file
71
cmd/rsa/key.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package rsa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"archgrid.xyz/ag/toolsbox/encryption/rsa"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rsaKeyOutputFile string
|
||||
)
|
||||
|
||||
var keyGenCmd = &cobra.Command{
|
||||
Use: "keygen",
|
||||
Short: "生成RSA密钥对",
|
||||
Long: `生成RSA密钥对,密钥对将会被保存到指定的文件中。`,
|
||||
Args: cobra.NoArgs,
|
||||
Run: keyGenExecute,
|
||||
}
|
||||
|
||||
func keyGenExecute(cmd *cobra.Command, args []string) {
|
||||
if len(rsaKeyOutputFile) == 0 {
|
||||
fmt.Println("必须指定密钥输出文件名的前缀。")
|
||||
return
|
||||
}
|
||||
keyPair, err := rsa.NewKeyPair(rsaKeyLength.IntoRSAKeyLength())
|
||||
if err != nil {
|
||||
fmt.Printf("无法生成RSA密钥对:%s\n", err)
|
||||
return
|
||||
}
|
||||
privateKeyBytes, err := rsa.EncodePrivateKey(keyPair.PrivateKey)
|
||||
if err != nil {
|
||||
fmt.Printf("无法编码RSA私钥:%s\n", err)
|
||||
return
|
||||
}
|
||||
privateKeyFile, err := os.OpenFile(rsaKeyOutputFile+".pri.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
fmt.Printf("无法创建RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
defer privateKeyFile.Close()
|
||||
_, err = privateKeyFile.Write(privateKeyBytes)
|
||||
if err != nil {
|
||||
fmt.Printf("无法写入RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
publicKeyBytes, err := rsa.EncodePublicKey(keyPair.PublicKey)
|
||||
if err != nil {
|
||||
fmt.Printf("无法编码RSA公钥:%s\n", err)
|
||||
return
|
||||
}
|
||||
publicKeyFile, err := os.OpenFile(rsaKeyOutputFile+".pub.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
fmt.Printf("无法创建RSA公钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
defer publicKeyFile.Close()
|
||||
_, err = publicKeyFile.Write(publicKeyBytes)
|
||||
if err != nil {
|
||||
fmt.Printf("无法写入RSA公钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("RSA密钥对已经保存到文件:%s.pri.pem, %s.pub.pem\n", rsaKeyOutputFile, rsaKeyOutputFile)
|
||||
}
|
||||
|
||||
func init() {
|
||||
keyGenCmd.Flags().StringVarP(&rsaKeyOutputFile, "output", "o", "", "用于保存密钥输出文件名的前缀。")
|
||||
rsaCmd.AddCommand(keyGenCmd)
|
||||
}
|
29
cmd/rsa/rsa.go
Normal file
29
cmd/rsa/rsa.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package rsa
|
||||
|
||||
import (
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rsaKeyLength types.RSAKeyBits = types.RSA2048
|
||||
publicKeyFile string
|
||||
privateKeyFile string
|
||||
)
|
||||
|
||||
var rsaCmd = &cobra.Command{
|
||||
Use: "rsa",
|
||||
Short: `RSA加密算法工具`,
|
||||
Long: `使用RSA算法对给定的内容进行加密或解密,生成RSA密钥。`,
|
||||
TraverseChildren: true,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rsaCmd.PersistentFlags().VarP(&rsaKeyLength, "key-length", "l", "RSA密钥长度,可选值有:1024, 2048,缺省值为2048。")
|
||||
rsaCmd.PersistentFlags().StringVarP(&publicKeyFile, "public-key", "u", "", "RSA公钥文件,用于加密。")
|
||||
rsaCmd.PersistentFlags().StringVarP(&privateKeyFile, "private-key", "r", "", "RSA私钥文件,用于解密。")
|
||||
}
|
||||
|
||||
func GetRsaCmd() *cobra.Command {
|
||||
return rsaCmd
|
||||
}
|
96
cmd/rsa/sign.go
Normal file
96
cmd/rsa/sign.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package rsa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/rsa"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rsaSignFile bool
|
||||
rsaSignInputFile string
|
||||
rsaSignEncoding types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var rsaSignCmd = &cobra.Command{
|
||||
Use: "sign",
|
||||
Short: `使用RSA算法对给定的内容进行签名`,
|
||||
Long: `使用RSA算法对给定的内容进行签名,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行签名,否则从命令行读取内容进行签名。`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: rsaSignExecute,
|
||||
}
|
||||
|
||||
func rsaSignExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
content []byte
|
||||
cert []byte
|
||||
keys *rsa.KeyPair
|
||||
err error
|
||||
)
|
||||
if len(privateKeyFile) > 0 {
|
||||
file, err := os.Open(privateKeyFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
cert, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Println("必须提供RSA私钥文件才能够签名。")
|
||||
return
|
||||
}
|
||||
keys, err = rsa.NewFromPEM(cert)
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析RSA私钥:%s\n", err)
|
||||
return
|
||||
}
|
||||
if rsaSignFile {
|
||||
if len(rsaSignInputFile) == 0 {
|
||||
fmt.Println("必须指定输入文件名。")
|
||||
return
|
||||
}
|
||||
file, err := os.Open(rsaSignInputFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
content, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if len(args) == 0 {
|
||||
fmt.Println("必须指定输入内容。")
|
||||
return
|
||||
}
|
||||
content = []byte(args[0])
|
||||
}
|
||||
signature, err := keys.Sign(content)
|
||||
if err != nil {
|
||||
fmt.Printf("无法签名:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch rsaSignEncoding {
|
||||
case types.ResultInHex:
|
||||
fmt.Printf("签名为:%s\n", hex.ToHex(signature))
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("签名为:%s\n", base64.ToBase64(signature))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rsaSignCmd.Flags().BoolVarP(&rsaSignFile, "file", "f", false, "指定对文件内容进行签名")
|
||||
rsaSignCmd.Flags().StringVarP(&rsaSignInputFile, "input", "i", "", "指定读入内容的文件名")
|
||||
rsaSignCmd.Flags().VarP(&rsaSignEncoding, "encoding", "e", "指定签名输出编码方式,可选值为hex或base64")
|
||||
rsaCmd.AddCommand(rsaSignCmd)
|
||||
}
|
119
cmd/rsa/verify.go
Normal file
119
cmd/rsa/verify.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package rsa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/rsa"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rsaVerifyFile bool
|
||||
rsaVerifyInputFile string
|
||||
rsaVerifyEncoding types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var rsaVerifyCmd = &cobra.Command{
|
||||
Use: "verify",
|
||||
Short: `使用RSA算法对给定的内容进行验证`,
|
||||
Long: `使用RSA算法对给定的内容进行验证,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行验证,否则从命令行读取内容进行验证。`,
|
||||
Args: cobra.MaximumNArgs(2),
|
||||
Run: rsaVerifyExecute,
|
||||
}
|
||||
|
||||
func rsaVerifyExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
content []byte
|
||||
cert []byte
|
||||
keys *rsa.KeyPair
|
||||
err error
|
||||
)
|
||||
if len(publicKeyFile) > 0 {
|
||||
file, err := os.Open(publicKeyFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开RSA公钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
cert, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取RSA公钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else if len(privateKeyFile) > 0 {
|
||||
file, err := os.Open(privateKeyFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
cert, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取RSA私钥文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Println("必须提供RSA密钥文件才能够验证。")
|
||||
return
|
||||
}
|
||||
keys, err = rsa.NewFromPEM(cert)
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析RSA密钥:%s\n", err)
|
||||
return
|
||||
}
|
||||
if rsaVerifyFile {
|
||||
if len(rsaVerifyInputFile) == 0 {
|
||||
fmt.Println("必须指定输入文件名。")
|
||||
return
|
||||
}
|
||||
file, err := os.Open(rsaVerifyInputFile)
|
||||
if err != nil {
|
||||
fmt.Printf("无法打开输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
content, err = io.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Printf("无法读取输入文件:%s\n", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if len(args) < 1 {
|
||||
fmt.Println("必须指定待验证内容。")
|
||||
return
|
||||
}
|
||||
content = []byte(args[0])
|
||||
}
|
||||
var signature []byte
|
||||
switch rsaVerifyEncoding {
|
||||
case types.ResultInHex:
|
||||
signature, err = hex.FromHex(args[len(args)-1])
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析签名内容:%s\n", err)
|
||||
return
|
||||
}
|
||||
case types.ResultInBase64:
|
||||
signature, err = base64.FromBase64(args[len(args)-1])
|
||||
if err != nil {
|
||||
fmt.Printf("无法解析签名内容:%s\n", err)
|
||||
return
|
||||
}
|
||||
default:
|
||||
fmt.Println("不支持的签名内容编码方式。")
|
||||
return
|
||||
}
|
||||
if err = keys.Verify(content, signature); err != nil {
|
||||
fmt.Printf("验证失败,%s\n", err)
|
||||
} else {
|
||||
fmt.Println("验证成功。")
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rsaVerifyCmd.Flags().BoolVarP(&rsaVerifyFile, "file", "f", false, "指定从文件中读入待验证内容")
|
||||
rsaVerifyCmd.Flags().StringVarP(&rsaVerifyInputFile, "input", "i", "", "指定读入内容的文件名")
|
||||
rsaVerifyCmd.Flags().VarP(&rsaVerifyEncoding, "encoding", "e", "指定签名内容的编码方式,可选值为hex和base64,默认为hex")
|
||||
rsaCmd.AddCommand(rsaVerifyCmd)
|
||||
}
|
54
cmd/sha1.go
Normal file
54
cmd/sha1.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/sha1"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
sha1File bool
|
||||
sha1Result types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var sha1Cmd = &cobra.Command{
|
||||
Use: "sha1",
|
||||
Short: "使用Sha1算法计算文件或者字符串的哈希值",
|
||||
Long: `使用Sha1算法计算文件或者字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: sha1HashExecute,
|
||||
}
|
||||
|
||||
func sha1HashExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
hashResult []byte
|
||||
err error
|
||||
)
|
||||
if sha1File {
|
||||
hashResult, err = sha1.SumFile(args[0])
|
||||
} else {
|
||||
hashResult = sha1.Sha1([]byte(args[0]))
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("散列计算失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch sha1Result {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
sha1Cmd.Flags().BoolVarP(&sha1File, "file", "f", false, "计算文件的Sha1哈希值。")
|
||||
sha1Cmd.Flags().VarP(&sha1Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
|
||||
rootCmd.AddCommand(sha1Cmd)
|
||||
}
|
54
cmd/sha256.go
Normal file
54
cmd/sha256.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/sha256"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
sha256File bool
|
||||
sha256Result types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var sha256Cmd = &cobra.Command{
|
||||
Use: "sha256",
|
||||
Short: "使用Sha256算法计算文件或字符串的哈希值。",
|
||||
Long: `使用Sha256算法计算文件或字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: sha256HashExecute,
|
||||
}
|
||||
|
||||
func sha256HashExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
hashResult []byte
|
||||
err error
|
||||
)
|
||||
if sha256File {
|
||||
hashResult, err = sha256.SumFile256(args[0])
|
||||
} else {
|
||||
hashResult = sha256.Sha256([]byte(args[0]))
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("散列计算失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch sha256Result {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
sha256Cmd.Flags().BoolVarP(&sha256File, "file", "f", false, "计算文件的Sha256哈希值。")
|
||||
sha256Cmd.Flags().VarP(&sha256Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
|
||||
rootCmd.AddCommand(sha256Cmd)
|
||||
}
|
54
cmd/sha512.go
Normal file
54
cmd/sha512.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/hash/sha512"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
sha512File bool
|
||||
sha512Result types.ResultEncoding = types.ResultInHex
|
||||
)
|
||||
|
||||
var sha512Cmd = &cobra.Command{
|
||||
Use: "sha512",
|
||||
Short: "使用Sha512算法计算文件或字符串的哈希值。",
|
||||
Long: `使用Sha512算法计算文件或字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: sha512HashExecute,
|
||||
}
|
||||
|
||||
func sha512HashExecute(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
hashResult []byte
|
||||
err error
|
||||
)
|
||||
if sha512File {
|
||||
hashResult, err = sha512.SumFile512(args[0])
|
||||
} else {
|
||||
hashResult = sha512.Sha512([]byte(args[0]))
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("散列计算失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch sha512Result {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
sha512Cmd.Flags().BoolVarP(&sha512File, "file", "f", false, "计算文件的Sha512哈希值。")
|
||||
sha512Cmd.Flags().VarP(&sha512Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
|
||||
rootCmd.AddCommand(sha512Cmd)
|
||||
}
|
51
cmd/spiral.go
Normal file
51
cmd/spiral.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/toolsbox/encryption/spiral"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var spiralCmd = &cobra.Command{
|
||||
Use: "spiral",
|
||||
Short: "螺旋随机密钥自解密算法工具",
|
||||
Long: `使用螺旋随机密钥自解密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
|
||||
}
|
||||
|
||||
var spiralEncodeCmd = &cobra.Command{
|
||||
Use: "encode",
|
||||
Short: "使用螺旋随机密钥自解密算法加密",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: spiralEncodeExecute,
|
||||
}
|
||||
|
||||
var spiralDecodeCmd = &cobra.Command{
|
||||
Use: "decode",
|
||||
Short: "使用螺旋随机密钥自解密算法解密",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: spiralDecodeExecute,
|
||||
}
|
||||
|
||||
func spiralEncodeExecute(cmd *cobra.Command, args []string) {
|
||||
encodedResult, err := spiral.Encrypt(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("加密失败,%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("加密结果:%s\n", encodedResult)
|
||||
}
|
||||
|
||||
func spiralDecodeExecute(cmd *cobra.Command, args []string) {
|
||||
decodedResult, err := spiral.Decrypt(args[0])
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败,%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("解密结果:%s\n", decodedResult)
|
||||
}
|
||||
|
||||
func init() {
|
||||
spiralCmd.AddCommand(spiralEncodeCmd, spiralDecodeCmd)
|
||||
rootCmd.AddCommand(spiralCmd)
|
||||
}
|
111
cmd/tdes.go
Normal file
111
cmd/tdes.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"archgrid.xyz/ag/tools/types"
|
||||
"archgrid.xyz/ag/toolsbox/encryption"
|
||||
"archgrid.xyz/ag/toolsbox/encryption/tdes"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/base64"
|
||||
"archgrid.xyz/ag/toolsbox/serialize/hex"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
tdesKey string
|
||||
tdesResult types.ResultEncoding = types.ResultInHex
|
||||
tdesPadding types.EncryptionPadding = types.PKCS7Padding
|
||||
)
|
||||
|
||||
var tdesCommand = &cobra.Command{
|
||||
Use: "3des",
|
||||
Short: "3DES加密算法工具",
|
||||
Long: `使用3DES加密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
|
||||
}
|
||||
|
||||
var tdesEncryptCmd = &cobra.Command{
|
||||
Use: "encrypt",
|
||||
Short: "使用3DES加密算法加密",
|
||||
Long: `使用3DES加密算法加密,支持三种填充方式:PKCS7、Zero和无填充。默认输出十六进制编码字符串。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: tdesEncryptExecute,
|
||||
}
|
||||
|
||||
func tdesEncryptExecute(cmd *cobra.Command, args []string) {
|
||||
var pad encryption.PaddingMode
|
||||
switch tdesPadding {
|
||||
case types.PKCS7Padding:
|
||||
pad = encryption.PKCS7Padding
|
||||
case types.ZeroPadding:
|
||||
pad = encryption.ZeroPadding
|
||||
case types.NoPadding:
|
||||
pad = encryption.NoPadding
|
||||
default:
|
||||
fmt.Printf("不支持的填充方式:%s\n", tdesPadding)
|
||||
return
|
||||
}
|
||||
encodedResult, err := tdes.Encrypt([]byte(args[0]), []byte(tdesKey), pad)
|
||||
if err != nil {
|
||||
fmt.Printf("加密失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
switch tdesResult {
|
||||
case types.ResultInBase64:
|
||||
fmt.Printf("加密结果:%s\n", base64.ToBase64(encodedResult))
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Printf("加密结果:%s\n", hex.ToHex(encodedResult))
|
||||
}
|
||||
}
|
||||
|
||||
var tdesDecryptCmd = &cobra.Command{
|
||||
Use: "decrypt",
|
||||
Short: "使用3DES加密算法解密",
|
||||
Long: `使用3DES加密算法解密,支持三种填充方式:PKCS7、Zero和无填充。默认输入十六进制编码字符串。`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: tdesDecryptExecute,
|
||||
}
|
||||
|
||||
func tdesDecryptExecute(cmd *cobra.Command, args []string) {
|
||||
var pad encryption.PaddingMode
|
||||
switch tdesPadding {
|
||||
case types.PKCS7Padding:
|
||||
pad = encryption.PKCS7Padding
|
||||
case types.ZeroPadding:
|
||||
pad = encryption.ZeroPadding
|
||||
case types.NoPadding:
|
||||
pad = encryption.NoPadding
|
||||
default:
|
||||
fmt.Printf("不支持的填充方式:%s\n", tdesPadding)
|
||||
return
|
||||
}
|
||||
var decodedResult []byte
|
||||
var err error
|
||||
switch tdesResult {
|
||||
case types.ResultInBase64:
|
||||
decodedResult, err = base64.FromBase64(args[0])
|
||||
case types.ResultInHex:
|
||||
fallthrough
|
||||
default:
|
||||
decodedResult, err = hex.FromHex(args[0])
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
decodedResult, err = tdes.Decrypt(decodedResult, []byte(tdesKey), pad)
|
||||
if err != nil {
|
||||
fmt.Printf("解密失败:%s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("解密结果:%s\n", decodedResult)
|
||||
}
|
||||
|
||||
func init() {
|
||||
tdesCommand.PersistentFlags().StringVarP(&tdesKey, "key", "k", "", "加密密钥。")
|
||||
tdesCommand.PersistentFlags().VarP(&tdesResult, "output", "o", "输入或输出使用编码格式,可选Base64或Hex,默认为Hex。")
|
||||
tdesCommand.PersistentFlags().VarP(&tdesPadding, "padding", "p", "填充方式,支持pkcs7、zero和none,默认为pkcs7。")
|
||||
tdesCommand.AddCommand(tdesEncryptCmd, tdesDecryptCmd)
|
||||
rootCmd.AddCommand(tdesCommand)
|
||||
}
|
28
cmd/verify_code.go
Normal file
28
cmd/verify_code.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
verifyCode "archgrid.xyz/ag/toolsbox/random/verify_code"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
verifyCodeLength int
|
||||
)
|
||||
|
||||
var verifyCodeCmd = &cobra.Command{
|
||||
Use: "verify_code",
|
||||
Short: "生成指定长度的随机验证码",
|
||||
Run: verifyCodeExecute,
|
||||
}
|
||||
|
||||
func verifyCodeExecute(cmd *cobra.Command, args []string) {
|
||||
code := verifyCode.RandStr(verifyCodeLength)
|
||||
fmt.Printf("生成的验证码为:%s\n", code)
|
||||
}
|
||||
|
||||
func init() {
|
||||
verifyCodeCmd.PersistentFlags().IntVarP(&verifyCodeLength, "length", "l", 10, "要生成的验证码长度,默认生成长度为10个字符的验证码")
|
||||
rootCmd.AddCommand(verifyCodeCmd)
|
||||
}
|
Reference in New Issue
Block a user