2016-02-14 11:00:28 +01:00
|
|
|
import std.getopt;
|
|
|
|
import std.string : format;
|
2016-02-28 14:25:58 +01:00
|
|
|
import std.stdio : stderr, writeln;
|
2016-02-14 11:00:28 +01:00
|
|
|
|
|
|
|
import cipher : Cipher;
|
|
|
|
import actions;
|
|
|
|
|
|
|
|
int main(string[] args)
|
|
|
|
{
|
2016-07-06 02:00:03 +02:00
|
|
|
bool[string] actions = ["genKey" : false, "encrypt" : false, "decrypt" : false, "benchmark" : false];
|
2016-02-14 11:00:28 +01:00
|
|
|
|
2016-02-16 19:36:24 +01:00
|
|
|
Cipher cipher = Cipher.chacha20;
|
2016-02-14 11:00:28 +01:00
|
|
|
string keyFileName = "symkey.asc";
|
|
|
|
bool armor;
|
|
|
|
|
2016-02-16 19:32:34 +01:00
|
|
|
GetoptResult result;
|
|
|
|
try
|
|
|
|
{
|
2016-07-04 15:00:52 +02:00
|
|
|
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"],
|
2016-07-06 02:00:03 +02:00
|
|
|
"benchmark|b", "Perform some benchmarks.", &actions["benchmark"],
|
2016-07-04 15:00:52 +02:00
|
|
|
"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);
|
2016-02-16 19:32:34 +01:00
|
|
|
}
|
2016-02-28 14:25:58 +01:00
|
|
|
catch (Exception e)
|
2016-02-16 19:32:34 +01:00
|
|
|
{
|
|
|
|
stderr.writeln(e.msg);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-02-14 11:00:28 +01:00
|
|
|
|
|
|
|
size_t numberOfActions;
|
2016-02-28 14:25:58 +01:00
|
|
|
foreach (value; actions.values)
|
2016-02-14 11:00:28 +01:00
|
|
|
numberOfActions += value;
|
|
|
|
|
2016-02-28 14:25:58 +01:00
|
|
|
if (numberOfActions == 1)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-02-28 14:25:58 +01:00
|
|
|
if (actions["genKey"])
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
generateKey(armor);
|
|
|
|
}
|
2016-02-28 14:25:58 +01:00
|
|
|
else if (actions["encrypt"])
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
encrypt(keyFileName, cipher, armor);
|
|
|
|
}
|
2016-02-28 14:25:58 +01:00
|
|
|
else if (actions["decrypt"])
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
decrypt(keyFileName, cipher, armor);
|
|
|
|
}
|
2016-07-06 02:00:03 +02:00
|
|
|
else if (actions["benchmark"])
|
|
|
|
{
|
|
|
|
bench();
|
|
|
|
}
|
2016-02-14 11:00:28 +01:00
|
|
|
}
|
2016-02-28 14:25:58 +01:00
|
|
|
catch (Exception e)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
stderr.writeln(e.msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-16 19:32:34 +01:00
|
|
|
result.helpWanted = true;
|
2016-02-14 11:00:28 +01:00
|
|
|
}
|
|
|
|
|
2016-02-28 14:25:58 +01:00
|
|
|
if (result.helpWanted)
|
2016-02-16 19:47:17 +01:00
|
|
|
printHelp(result.options);
|
2016-02-16 19:32:34 +01:00
|
|
|
|
2016-02-14 11:00:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2016-02-16 19:32:34 +01:00
|
|
|
|
|
|
|
void printHelp(Option[] options)
|
|
|
|
{
|
2016-07-04 15:00:52 +02:00
|
|
|
defaultGetoptPrinter("Usage: ./learncrypt [options]\n\nCommon options:", options[$ - 1 .. $]);
|
|
|
|
defaultGetoptPrinter("\nGlobal options:", options[$ - 2 .. $ - 1]);
|
2016-07-06 02:00:03 +02:00
|
|
|
defaultGetoptPrinter("\nActions:", options[0 .. 4]);
|
|
|
|
defaultGetoptPrinter("\nAction options:", options[4 .. 5]);
|
2016-02-16 19:32:34 +01:00
|
|
|
}
|