Skip to content

Commit

Permalink
feat: 增加cli的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Feb 15, 2024
1 parent e844288 commit 80fd980
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
*.exe
28 changes: 28 additions & 0 deletions cmd/about.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
54 changes: 54 additions & 0 deletions cmd/decrypt.go
Original file line number Diff line number Diff line change
@@ -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: "",
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
}
87 changes: 87 additions & 0 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
@@ -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: "",
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
}
37 changes: 37 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
21 changes: 21 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/cryptography-research-lab/go-qwerty-cipher/cmd"

func main() {
cmd.Execute()
}
4 changes: 2 additions & 2 deletions pkg/qwerty_cipher/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/qwerty_cipher/qwerty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 80fd980

Please sign in to comment.