commands

package
v0.0.0-...-0f04028 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DECYPH_CYPHER = 0
View Source
const DECYPH_INPUT = 1
View Source
const OS_FILE_MODE = 0644
View Source
const STATUS_ERR = 1
View Source
const STATUS_OK = 0

Variables

View Source
var Decypher = cli.NewCommand("decypher", "Decypher a string passed in the console or a file").
	WithArg(
		cli.NewArg("cypher", "The cypher to use (use > to chain cyphers e.g.: 'caesar>vigniere')").
			WithType(cli.TypeString)).
	WithArg(
		cli.NewArg("input", "The plaintext to decypher; Reads from STDIN if left empty").
			WithType(cli.TypeString).
			AsOptional()).
	WithOption(
		cli.NewOption("direct-input", "Decyphers the input directly instead of opening it as a file.").
			WithChar('d').
			WithType(cli.TypeBool)).
	WithOption(
		cli.NewOption("key", "Key to use for decyphering").
			WithChar('k').
			WithType(cli.TypeString)).
	WithOption(
		cli.NewOption("output", "File to put decyphered text").
			WithChar('o').
			WithType(cli.TypeString)).
	WithAction(func(args []string, options map[string]string) int {

		cyphName := args[DECYPH_CYPHER]
		input := ""
		if len(args) > 1 {
			input = args[DECYPH_INPUT]
		}

		keyStr, keyExists := options["key"]
		key := cypher.ParseKey(keyStr)
		if !keyExists {
			key = cypher.NilKey
		}

		_, directInput := options["direct-input"]
		if !directInput && input != "" {
			bytes, err := ioutil.ReadFile(input)
			if err != nil {
				fmt.Println("Error: ", err)
				return STATUS_ERR
			}
			input = string(bytes)
		}

		if input == "" {
			crlfReader := crlf.NewReader(bufio.NewReader(os.Stdin))
			bytes, err := ioutil.ReadAll(crlfReader)
			if err != nil {
				fmt.Println("Error: ", err)
				return STATUS_ERR
			}
			input = string(bytes)

			input = strings.TrimSuffix(input, "\n")
		}

		cyph, err := turing.RegistryGetCypher(cyphName)
		if err != nil {
			fmt.Println("Error: ", err)
			return STATUS_ERR
		}

		plaintext := cyph.Decypher(input, key)

		outFile, isOutToFile := options["output"]
		if isOutToFile {
			ioutil.WriteFile(outFile, []byte(plaintext), OS_FILE_MODE)
		} else {
			fmt.Printf("Decyphered Plaintext: \"%v\"\n", plaintext)
		}

		return STATUS_OK
	})

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL