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

2 years ago
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{
10 months ago
Use: "aescfb",
Short: "使用自定义aes-cfb处理文件或字符串",
Long: "使用自定义aes-cfb处理文件或字符串",
2 years ago
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 {
10 months ago
data, err := EncodeStr(args[0], byteKey)
if err != nil {
starlog.Criticalln(err)
return
}
2 years ago
fmt.Println(data)
} else {
10 months ago
data, err := DecodeStr(args[0], byteKey)
if err != nil {
starlog.Criticalln(err)
return
}
2 years ago
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解码")
}