learncrypto/source/app.d

86 lines
2.1 KiB
D
Raw Normal View History

2016-02-14 11:00:28 +01:00
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;
2016-02-14 11:00:28 +01:00
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"],
"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;
}
2016-02-14 11:00:28 +01:00
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
{
result.helpWanted = true;
2016-02-14 11:00:28 +01:00
}
if(result.helpWanted)
printHelp(result.options);
2016-02-14 11:00:28 +01:00
return 0;
}
void printHelp(Option[] options)
{
2016-02-16 19:56:32 +01:00
defaultGetoptPrinter("Usage: ./learncrypt [options]\n\nCommon options:",
options[$-1..$]);
defaultGetoptPrinter("\nGlobal options:",
options[$-2..$-1]);
defaultGetoptPrinter("\nActions:",
options[0..3]);
defaultGetoptPrinter("\nAction options:",
options[3..5]);
}