-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e844288
commit 80fd980
Showing
9 changed files
with
239 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
.idea | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters