learncrypto/source/app.d
2016-08-01 01:42:29 +02:00

83 lines
2.2 KiB
D

import std.getopt;
import std.string : format;
import std.stdio : stderr, writeln;
import cipher : Cipher;
import actions;
int main(string[] args)
{
bool[string] actions = ["genKey" : false, "encrypt" : false, "decrypt"
: false, "benchmark" : false];
Cipher cipher = Cipher.chacha20;
string keyFileName = "symkey.asc";
bool armor;
GetoptResult result;
try
{
result = getopt(args, std.getopt.config.bundling, "gen-key|g",
"Generate a new 256 bit key.", &actions["genKey"],
"encrypt|e", "Encrypt a message.", &actions["encrypt"], "decrypt|d",
"Decrypt a message.", &actions["decrypt"], "benchmark|b",
"Perform some benchmarks.", &actions["benchmark"], "cipher|c",
"The cipher to use (default: %s).".format(cipher), &cipher,
"key|k", "The file which contains the key (default: %s).".format(keyFileName),
&keyFileName, "armor|a", "use ascii-armored I/O.", &armor);
}
catch (Exception e)
{
stderr.writeln(e.msg);
return 1;
}
size_t numberOfActions;
foreach (value; actions.values)
numberOfActions += value;
if (numberOfActions == 1)
{
try
{
if (actions["genKey"])
{
generateKey(armor);
}
else if (actions["encrypt"])
{
encrypt(keyFileName, cipher, armor);
}
else if (actions["decrypt"])
{
decrypt(keyFileName, cipher, armor);
}
else if (actions["benchmark"])
{
bench();
}
}
catch (Exception e)
{
stderr.writeln(e.msg);
return 1;
}
}
else
{
result.helpWanted = true;
}
if (result.helpWanted)
printHelp(result.options);
return 0;
}
void printHelp(Option[] options)
{
defaultGetoptPrinter("Usage: ./learncrypt [options]\n\nCommon options:", options[$ - 1 .. $]);
defaultGetoptPrinter("\nGlobal options:", options[$ - 2 .. $ - 1]);
defaultGetoptPrinter("\nActions:", options[0 .. 4]);
defaultGetoptPrinter("\nAction options:", options[4 .. 5]);
}