diff --git a/.gitignore b/.gitignore index 723ef36..d7c3743 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +*.exe \ No newline at end of file diff --git a/cmd/about.go b/cmd/about.go new file mode 100644 index 0000000..bd272e4 --- /dev/null +++ b/cmd/about.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + "github.com/fatih/color" + go_StringBuilder "github.com/golang-infrastructure/go-StringBuilder" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(aboutCmd) +} + +var aboutCmd = &cobra.Command{ + Use: "about", + Short: "About this tool github, about author, blabla.", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + msg := go_StringBuilder.New(). + AppendString("\n\n\n"). + AppendString(fmt.Sprintf("%20s", "Repo")).AppendString(" : ").AppendString("https://github.com/cryptography-research-lab/go-qwerty-cipher\n\n"). + AppendString(fmt.Sprintf("%20s", "Problem feedback")).AppendString(" : ").AppendString("https://github.com/cryptography-research-lab/go-qwerty-cipher/issues\n\n"). + AppendString(fmt.Sprintf("%20s", "Author")).AppendString(" : ").AppendString("CC11001100\n"). + AppendString("\n\n\n"). + String() + color.HiGreen(msg) + }, +} diff --git a/cmd/decrypt.go b/cmd/decrypt.go new file mode 100644 index 0000000..f89c543 --- /dev/null +++ b/cmd/decrypt.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "fmt" + "github.com/cryptography-research-lab/go-qwerty-cipher/pkg/qwerty_cipher" + "github.com/spf13/cobra" +) + +var ciphertext string + +func init() { + + decryptCmd.Flags().StringVarP(&keyboard, "keyboard", "k", "", "custom keyboard") + decryptCmd.Flags().StringVarP(&ciphertext, "ciphertext", "c", "", "ciphertext to decrypt") + + rootCmd.AddCommand(decryptCmd) +} + +var decryptCmd = &cobra.Command{ + Use: "decrypt", + Short: "Decrypt ciphertext using qwerty encryption algorithm", + Long: ``, + RunE: func(cmd *cobra.Command, args []string) error { + + keyboardLayout := parseKeyboardLayout(keyboard) + + if err := checkDecryptParams(keyboardLayout, ciphertext); err != nil { + return err + } + + plaintext, err := qwerty_cipher.Decrypt(ciphertext, keyboardLayout.TransformToDecrypt()) + if err != nil { + return err + } + fmt.Println("Decrypt Result: " + plaintext) + return nil + }, +} + +// 检查加密所需的参数是否合法 +func checkDecryptParams(keyboardLayout qwerty_cipher.KeyboardLayout, ciphertext string) error { + + // 密文不允许为空 + if ciphertext == "" { + return fmt.Errorf("ciphertext must not empty") + } + + // 键盘布局可以不指定,仅当指定的时候才检查布局是否合法 + if err := keyboardLayout.Check(); err != nil { + return fmt.Errorf("keyboard layout %s is not ok: %s", string(keyboardLayout), err.Error()) + } + + return nil +} diff --git a/cmd/encrypt.go b/cmd/encrypt.go new file mode 100644 index 0000000..c9978de --- /dev/null +++ b/cmd/encrypt.go @@ -0,0 +1,87 @@ +package cmd + +import ( + "fmt" + "github.com/cryptography-research-lab/go-qwerty-cipher/pkg/qwerty_cipher" + "github.com/spf13/cobra" + "strings" +) + +// 可以指定键盘布局 +var keyboard string + +// DefaultKeyboardLayout 默认的键盘布局为qwerty布局 +var DefaultKeyboardLayout = qwerty_cipher.QwertyKeyboardLayout + +// 要加密的明文 +var plaintext string + +func init() { + + encryptCmd.Flags().StringVarP(&keyboard, "keyboard", "k", "", "custom keyboard") + encryptCmd.Flags().StringVarP(&plaintext, "plaintext", "t", "", "plaintext to encrypt") + + rootCmd.AddCommand(encryptCmd) +} + +var encryptCmd = &cobra.Command{ + Use: "encrypt", + Short: "Encrypt plaintext using qwerty encryption algorithm", + Long: ``, + RunE: func(cmd *cobra.Command, args []string) error { + + // 解析键盘布局 + keyboardLayout := parseKeyboardLayout(keyboard) + + // 参数检查 + if err := checkEncryptParams(keyboardLayout, plaintext); err != nil { + return err + } + + plaintext, err := qwerty_cipher.Encrypt(plaintext, keyboardLayout) + if err != nil { + return err + } + fmt.Println("Encrypt Result: " + plaintext) + return nil + }, +} + +// 解析传入的键盘布局参数,如果没有传的话则使用默认的 +func parseKeyboardLayout(keyboard string) qwerty_cipher.KeyboardLayout { + + // 如果没有指定的话,则使用默认的键盘布局 + if keyboard == "" { + return DefaultKeyboardLayout + } + + // 看看是不是预置的几个布局 + switch strings.ToLower(keyboard) { + case "qwerty": + return qwerty_cipher.QwertyKeyboardLayout + case "qwertz": + return qwerty_cipher.QwertzKeyboardLayout + case "azerty": + return qwerty_cipher.AzertyKeyboardLayout + default: + // 指定的不是预置的键盘布局,说明是自定义的键盘布局,则直接转为键盘布局使用 + return qwerty_cipher.KeyboardLayout(keyboard) + } + +} + +// 检查加密所需的参数是否合法 +func checkEncryptParams(keyboardLayout qwerty_cipher.KeyboardLayout, plaintext string) error { + + // 要加密的明文不允许为空 + if plaintext == "" { + return fmt.Errorf("plaintext must not empty") + } + + // 键盘布局可以不指定,仅当指定的时候才检查布局是否合法 + if err := keyboardLayout.Check(); err != nil { + return fmt.Errorf("keyboard layout %s is not ok: %s", string(keyboardLayout), err.Error()) + } + + return nil +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..281f6ad --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "fmt" + "github.com/fatih/color" + cc "github.com/ivanpirog/coloredcobra" + "github.com/spf13/cobra" + "os" +) + +var rootCmd = &cobra.Command{ + Use: "qwerty-cipher", + Short: "", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + _ = cmd.Help() + }, +} + +func Execute() { + + cc.Init(&cc.Config{ + RootCmd: rootCmd, + Headings: cc.HiCyan + cc.Bold + cc.Underline, + Commands: cc.HiYellow + cc.Bold, + Example: cc.Italic, + ExecName: cc.Bold, + Flags: cc.Bold, + }) + // 官方文档没提,但是似乎不加这一句会乱码... + rootCmd.SetOut(color.Output) + + if err := rootCmd.Execute(); err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..6edb2b6 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +const Version = "v0.1" + +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show the version number of big ip hacker", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + color.HiGreen("version %s", Version) + }, +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..595ee1f --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/cryptography-research-lab/go-qwerty-cipher/cmd" + +func main() { + cmd.Execute() +} diff --git a/pkg/qwerty_cipher/layout.go b/pkg/qwerty_cipher/layout.go index df25e1b..506862f 100644 --- a/pkg/qwerty_cipher/layout.go +++ b/pkg/qwerty_cipher/layout.go @@ -4,8 +4,8 @@ package qwerty_cipher // 具体的例子详见 QwertyKeyboardLayout 实现 type KeyboardLayout []rune -// 检查当前的键盘布局映射表是否合法 -func (x KeyboardLayout) check() error { +// Check 检查当前的键盘布局映射表是否合法 +func (x KeyboardLayout) Check() error { // 如果长度都不对,则没必要继续下去了 if len(x) != 26 { diff --git a/pkg/qwerty_cipher/qwerty.go b/pkg/qwerty_cipher/qwerty.go index 6e3e502..ab2c0c4 100644 --- a/pkg/qwerty_cipher/qwerty.go +++ b/pkg/qwerty_cipher/qwerty.go @@ -10,7 +10,7 @@ func Encrypt(plaintext string, keyboardLayout ...KeyboardLayout) (string, error) keyboardLayout = variable_parameter.SetDefaultParam(keyboardLayout, QwertyKeyboardLayout) // 参数校验 - if err := keyboardLayout[0].check(); err != nil { + if err := keyboardLayout[0].Check(); err != nil { return "", err }