learncrypto/source/app.d
2016-02-14 11:00:28 +01:00

73 lines
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 ];
Cipher cipher = Cipher.chacha20;
string keyFileName = "symkey.asc";
bool armor;
auto helpInformation = 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"],
"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);
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);
}
}
catch(Exception e)
{
stderr.writeln(e.msg);
return 1;
}
}
else
{
helpInformation.helpWanted = true;
}
if(helpInformation.helpWanted)
{
defaultGetoptPrinter("Usage: mycrypt [options]\n\nCommon options:",
helpInformation.options[$-1..$]);
defaultGetoptPrinter("\nGlobal options:",
helpInformation.options[$-2..$-1]);
defaultGetoptPrinter("\nActions:",
helpInformation.options[0..3]);
defaultGetoptPrinter("\nAction options:",
helpInformation.options[3..5]);
}
return 0;
}