redone spf13/cobra commands for transposition

commands now make much more sense.  Everything has been redone and all
of transpositions subcommands have been implemented
This commit is contained in:
Derek McQuay 2016-03-11 21:31:57 -08:00
parent 7dd108f4ba
commit 079d3d524b

193
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"os"
"time" "time"
"s.mcquay.me/dm/psyfer/psyfer" "s.mcquay.me/dm/psyfer/psyfer"
@ -15,17 +16,18 @@ func init() {
} }
func main() { func main() {
psyfer.ReadConfig() //psyfer.ReadConfig()
psyfer.Substitution("hello") //psyfer.Substitution("hello")
var input string var input string
var cipher string var cipher string
var cipType string var cipType string
var key string var key string
var decrypt bool
var encrypt = &cobra.Command{ var sub = &cobra.Command{
Use: "encrypt -c [cipher] -k [key] -i [input]", Use: "sub mode -c [cipher] -k [key] -i [input]",
Short: "encrypt string", Short: "substitution cipher",
Long: `encrypt given string`, Long: `perform substitution cipher`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if input != "" { if input != "" {
switch { switch {
@ -53,33 +55,81 @@ func main() {
}, },
} }
var decrypt = &cobra.Command{ var trans = &cobra.Command{
Use: "decrypt -c [cipher] -k [key] -i [input]", Use: "trans mode",
Short: "decrypt string", Short: "transposition cipher",
Long: `decrypt given string`, Long: `perform transposition cipher`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if input != "" { fmt.Println("missing input, see -h (--help) for more info")
switch { },
case cipher == "transposition":
switch {
case cipType == "random":
fmt.Println("random doesn't have a decryptable solution")
case cipType == "rail":
fmt.Println(psyfer.DeTransposeRailFence(input))
case cipType == "split":
fmt.Println(psyfer.DeTransposeSplit(input))
default:
fmt.Println("Missing cipher sub type (random, rail, split)")
} }
case cipher == "substitution":
fmt.Println("substitution") var aes = &cobra.Command{
case cipher == "vigenere": Use: "aes mode -c [cipher] -k [key] -i [input]",
fmt.Println("vigenere") Short: "aes cipher",
default: Long: `perform aes cipher`,
fmt.Println("Must choose transposition, substitution, or vigenere") Run: func(cmd *cobra.Command, args []string) {
fmt.Println("missing input, see -h (--help) for more info")
},
} }
var vig = &cobra.Command{
Use: "vig mode -c [cipher] -k [key] -i [input]",
Short: "vignenere cipher",
Long: `perform vigenere cipher`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("missing input, see -h (--help) for more info")
},
}
var random = &cobra.Command{
Use: "random",
Short: "randomly transpose",
Long: `randomly transposes input`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide an input string")
os.Exit(1)
}
for _, arg := range args {
fmt.Println(psyfer.TransposeRandom(arg))
}
},
}
var railfence = &cobra.Command{
Use: "railfence",
Short: "railfence transpose",
Long: `performs railfence transposition on input`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide an input string")
os.Exit(1)
}
for _, arg := range args {
if decrypt {
fmt.Println(psyfer.DeTransposeRailFence(arg))
} else { } else {
fmt.Println("Missing input") fmt.Println(psyfer.TransposeRailFence(arg))
}
}
},
}
var split = &cobra.Command{
Use: "split",
Short: "split transpose",
Long: `performs split transposition on input`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide an input string")
os.Exit(1)
}
for _, arg := range args {
if decrypt {
fmt.Println(psyfer.DeTransposeSplit(arg))
} else {
fmt.Println(psyfer.TransposeSplit(arg))
}
} }
}, },
} }
@ -93,28 +143,43 @@ func main() {
}, },
} }
encrypt.Flags().StringVarP( split.Flags().BoolVarP(
&decrypt,
"decrypt",
"d",
false,
"decrypt",
)
railfence.Flags().BoolVarP(
&decrypt,
"decrypt",
"d",
false,
"decrypt",
)
sub.Flags().StringVarP(
&input, &input,
"input", "input",
"i", "i",
"", "",
"string to be encrypted", "string to be encrypted",
) )
encrypt.Flags().StringVarP( sub.Flags().StringVarP(
&cipher, &cipher,
"cipher", "cipher",
"c", "c",
"", "",
"cipher to be used (transposition, substitution, vigenere)", "cipher to be used (transposition, substitution, vigenere)",
) )
encrypt.Flags().StringVarP( sub.Flags().StringVarP(
&key, &key,
"key", "key",
"k", "k",
"", "",
"key to be used", "key to be used",
) )
encrypt.Flags().StringVarP( sub.Flags().StringVarP(
&cipType, &cipType,
"type", "type",
"t", "t",
@ -122,37 +187,41 @@ func main() {
"sub type of cipher", "sub type of cipher",
) )
decrypt.Flags().StringVarP( //decrypt.Flags().StringVarP(
&input, // &input,
"input", // "input",
"i", // "i",
"", // "",
"string to be encrypted", // "string to be encrypted",
) //)
decrypt.Flags().StringVarP( //decrypt.Flags().StringVarP(
&cipher, // &cipher,
"cipher", // "cipher",
"c", // "c",
"", // "",
"cipher to be used (transposition, substitution, vigenere)", // "cipher to be used (transposition, substitution, vigenere)",
) //)
decrypt.Flags().StringVarP( //decrypt.Flags().StringVarP(
&key, // &key,
"key", // "key",
"k", // "k",
"", // "",
"key to be used", // "key to be used",
) //)
decrypt.Flags().StringVarP( //decrypt.Flags().StringVarP(
&cipType, // &cipType,
"type", // "type",
"t", // "t",
"", // "",
"sub type of cipher", // "sub type of cipher",
) //)
var rootCmd = &cobra.Command{Use: "app"} var rootCmd = &cobra.Command{Use: "app"}
rootCmd.AddCommand(encrypt, decrypt, crack) rootCmd.AddCommand(sub, aes, trans, vig)
sub.AddCommand(crack)
aes.AddCommand(crack)
trans.AddCommand(random, railfence, split)
vig.AddCommand(crack)
rootCmd.Execute() rootCmd.Execute()
//fmt.Println(psyfer.DeTransposeRailFence(psyfer.TransposeRailFence("helloworld"))) //fmt.Println(psyfer.DeTransposeRailFence(psyfer.TransposeRailFence("helloworld")))
//fmt.Println(psyfer.DeTransposeRailFence(psyfer.TransposeRailFence("1"))) //fmt.Println(psyfer.DeTransposeRailFence(psyfer.TransposeRailFence("1")))