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/aes/cmd.go

102 lines
2.5 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package aes
import (
"encoding/hex"
"fmt"
"os"
"path/filepath"
"b612.me/starcrypto"
"b612.me/starlog"
"b612.me/staros"
"github.com/spf13/cobra"
)
var Cmd = &cobra.Command{
Use: "aescfb",
Short: "使用自定义aes-cfb处理文件或字符串",
Long: "使用自定义aes-cfb处理文件或字符串",
Run: func(this *cobra.Command, args []string) {
var err error
ok, _ := this.Flags().GetBool("file")
de, _ := this.Flags().GetBool("decode")
key, _ := this.Flags().GetString("key")
keyfile, _ := this.Flags().GetString("keyfile")
if len(key) == 0 && len(keyfile) == 0 {
starlog.Errorln("请指定指定Key或KeyFile")
os.Exit(1)
}
if len(key) != 0 && len(keyfile) != 0 {
starlog.Errorln("不能同时指定Key与KeyFile")
os.Exit(1)
}
if len(args) != 1 {
starlog.Criticalln("参数不足,请输入文件地址或字符串")
this.Help()
os.Exit(2)
}
var byteKey []byte
if len(key) != 0 {
byteKey = []byte(key)
} else {
if !staros.Exists(keyfile) {
starlog.Errorln("keyfile不存在", keyfile)
os.Exit(3)
}
fmt.Println("读取Key文件中……")
tmpstr, err := starcrypto.FileSum(keyfile, "sha256", func(ok float64) { fmt.Printf("完成读取%.2f\r", ok) })
if err != nil {
starlog.Errorln("keyfile读取失败", err)
os.Exit(4)
}
fmt.Println()
byteKey, _ = hex.DecodeString(tmpstr)
}
if ok {
path, _ := this.Flags().GetString("out")
if path == "" {
ext := filepath.Ext(args[0])
if ext != "" {
path = args[0][:len(args[0])-len(ext)] + ".aescfb" + ext
} else {
path = args[0] + ".aescfb"
}
}
if !de {
err = EncodeFile(args[0], path, byteKey)
} else {
err = DecodeFile(args[0], path, byteKey)
}
} else {
if !de {
data, err := EncodeStr(args[0], byteKey)
if err != nil {
starlog.Criticalln(err)
return
}
fmt.Println(data)
} else {
data, err := DecodeStr(args[0], byteKey)
if err != nil {
starlog.Criticalln(err)
return
}
fmt.Println(string(data))
}
}
if err != nil {
starlog.Criticalln(err)
return
}
},
}
func init() {
Cmd.Flags().BoolP("file", "f", false, "aes-cfb处理文件")
Cmd.Flags().StringP("out", "o", "", "文件加解密输出地址")
Cmd.Flags().StringP("key", "k", "", "加解密Key字符串")
Cmd.Flags().StringP("keyfile", "s", "", "加解密Key文件不能与key字符串同时选择")
Cmd.Flags().BoolP("decode", "d", false, "进行aes-cfb解码")
}