Added @safe, nothorw, pure, @nogc attributes when possible. Catch exception thrown by getopt
This commit is contained in:
parent
a2ea77f667
commit
cfd9651b65
5 changed files with 66 additions and 77 deletions
56
source/app.d
56
source/app.d
|
@ -11,19 +11,28 @@ int main(string[] args)
|
||||||
"encrypt" : false,
|
"encrypt" : false,
|
||||||
"decrypt" : false ];
|
"decrypt" : false ];
|
||||||
|
|
||||||
Cipher cipher = Cipher.chacha20;
|
Cipher cipher = Cipher.salsa20;
|
||||||
string keyFileName = "symkey.asc";
|
string keyFileName = "symkey.asc";
|
||||||
bool armor;
|
bool armor;
|
||||||
|
|
||||||
auto helpInformation = getopt(
|
GetoptResult result;
|
||||||
args,
|
try
|
||||||
std.getopt.config.bundling,
|
{
|
||||||
"gen-key|g", "Generate a new 256 bit key.", &actions["genKey"],
|
result = getopt(
|
||||||
"encrypt|e", "Encrypt a message.", &actions["encrypt"],
|
args,
|
||||||
"decrypt|d", "Decrypt a message.", &actions["decrypt"],
|
std.getopt.config.bundling,
|
||||||
"cipher|c", "The cipher to use (default: %s).".format(cipher), &cipher,
|
"gen-key|g", "Generate a new 256 bit key.", &actions["genKey"],
|
||||||
"key|k", "The file which contains the key (default: %s).".format(keyFileName), &keyFileName,
|
"encrypt|e", "Encrypt a message.", &actions["encrypt"],
|
||||||
"armor|a", "use ascii-armored I/O.", &armor);
|
"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;
|
||||||
|
}
|
||||||
|
|
||||||
size_t numberOfActions;
|
size_t numberOfActions;
|
||||||
foreach(value; actions.values)
|
foreach(value; actions.values)
|
||||||
|
@ -54,19 +63,22 @@ int main(string[] args)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
helpInformation.helpWanted = true;
|
result.helpWanted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(helpInformation.helpWanted)
|
printHelp(result.options);
|
||||||
{
|
|
||||||
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;
|
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..3]);
|
||||||
|
defaultGetoptPrinter("\nAction options:",
|
||||||
|
options[3..5]);
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
module bitmanip;
|
module bitmanip;
|
||||||
|
|
||||||
private import std.traits : isUnsigned;
|
private import std.traits : isUnsigned;
|
||||||
|
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative;
|
||||||
|
|
||||||
UIntType rotateLeft(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure
|
UIntType rotateLeft(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure @safe
|
||||||
if(isUnsigned!UIntType)
|
if(isUnsigned!UIntType)
|
||||||
{
|
{
|
||||||
auto reducedLen = len % (8 * UIntType.sizeof);
|
auto reducedLen = len % (8 * UIntType.sizeof);
|
||||||
|
@ -19,7 +20,7 @@ unittest
|
||||||
assert(test[1].rotateLeft(7) == 128);
|
assert(test[1].rotateLeft(7) == 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
UIntType rotateRight(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure
|
UIntType rotateRight(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure @safe
|
||||||
if(isUnsigned!UIntType)
|
if(isUnsigned!UIntType)
|
||||||
{
|
{
|
||||||
auto reducedLen = len % (8 * UIntType.sizeof);
|
auto reducedLen = len % (8 * UIntType.sizeof);
|
||||||
|
@ -35,3 +36,26 @@ unittest
|
||||||
assert(test[2].rotateRight(1) == 64);
|
assert(test[2].rotateRight(1) == 64);
|
||||||
assert(test[2].rotateRight(7) == 1);
|
assert(test[2].rotateRight(7) == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
alias littleEndianInv = nativeToLittleEndian;
|
||||||
|
|
||||||
|
uint littleEndian(in ubyte[] input) @safe pure nothrow @nogc
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(input.length == uint.sizeof);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
ubyte[uint.sizeof] buf = input;
|
||||||
|
return littleEndianToNative!uint(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
ubyte[] test0 = [0, 0, 0, 0];
|
||||||
|
ubyte[] test1 = [86, 75, 30, 9];
|
||||||
|
ubyte[] test2 = [255, 255, 255, 250];
|
||||||
|
assert(littleEndian(test0) == 0x00000000);
|
||||||
|
assert(littleEndian(test1) == 0x091e4b56);
|
||||||
|
assert(littleEndian(test2) == 0xfaffffff);
|
||||||
|
}
|
||||||
|
|
|
@ -212,30 +212,7 @@ enum string colRound(alias _x00, alias _x01, alias _x02, alias _x03,
|
||||||
0x481c2027, 0x53a8e4b5, 0x4c1f89c5, 0x3f78c9c8]);
|
0x481c2027, 0x53a8e4b5, 0x4c1f89c5, 0x3f78c9c8]);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
alias littleEndianInv = nativeToLittleEndian;
|
ubyte[] chacha20(in ubyte[] input) @safe nothrow pure
|
||||||
|
|
||||||
uint littleEndian(in ubyte[] input)
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(input.length == uint.sizeof);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
ubyte[uint.sizeof] buf = input;
|
|
||||||
return littleEndianToNative!uint(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
unittest
|
|
||||||
{
|
|
||||||
ubyte[] test0 = [0, 0, 0, 0];
|
|
||||||
ubyte[] test1 = [86, 75, 30, 9];
|
|
||||||
ubyte[] test2 = [255, 255, 255, 250];
|
|
||||||
assert(littleEndian(test0) == 0x00000000);
|
|
||||||
assert(littleEndian(test1) == 0x091e4b56);
|
|
||||||
assert(littleEndian(test2) == 0xfaffffff);
|
|
||||||
}
|
|
||||||
|
|
||||||
ubyte[] chacha20(in ubyte[] input)
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(input.length == 64);
|
assert(input.length == 64);
|
||||||
|
@ -332,7 +309,7 @@ enum ubyte[4] σ1 = [110, 100, 32, 51];
|
||||||
enum ubyte[4] σ2 = [ 50, 45, 98, 121];
|
enum ubyte[4] σ2 = [ 50, 45, 98, 121];
|
||||||
enum ubyte[4] σ3 = [116, 101, 32, 107];
|
enum ubyte[4] σ3 = [116, 101, 32, 107];
|
||||||
|
|
||||||
ubyte[] chacha20Exp(in ubyte[] key, in ubyte[] n)
|
ubyte[] chacha20Exp(in ubyte[] key, in ubyte[] n) @safe nothrow pure
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(key.length == 32);
|
assert(key.length == 32);
|
||||||
|
|
|
@ -19,9 +19,9 @@ auto cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
|
||||||
final switch(cipher)
|
final switch(cipher)
|
||||||
{
|
{
|
||||||
case Cipher.salsa20:
|
case Cipher.salsa20:
|
||||||
return salsa20Cipher(range, key, nonce);
|
return range.salsa20Cipher(key, nonce);
|
||||||
|
|
||||||
case Cipher.chacha20:
|
case Cipher.chacha20:
|
||||||
return salsa20Cipher(range, key, nonce);
|
return range.salsa20Cipher(key, nonce);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ module salsa20;
|
||||||
private import std.traits : isUnsigned;
|
private import std.traits : isUnsigned;
|
||||||
private import std.string : format;
|
private import std.string : format;
|
||||||
private import std.range : isInputRange, isForwardRange, ElementType;
|
private import std.range : isInputRange, isForwardRange, ElementType;
|
||||||
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative;
|
|
||||||
private import std.array;
|
private import std.array;
|
||||||
|
|
||||||
private import bitmanip;
|
private import bitmanip;
|
||||||
|
@ -212,30 +211,7 @@ unittest
|
||||||
0x481c2027, 0x53a8e4b5, 0x4c1f89c5, 0x3f78c9c8]);
|
0x481c2027, 0x53a8e4b5, 0x4c1f89c5, 0x3f78c9c8]);
|
||||||
}
|
}
|
||||||
|
|
||||||
alias littleEndianInv = nativeToLittleEndian;
|
ubyte[] salsa20(in ubyte[] input) @save pure nothrow @safe
|
||||||
|
|
||||||
uint littleEndian(in ubyte[] input)
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(input.length == uint.sizeof);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
ubyte[uint.sizeof] buf = input;
|
|
||||||
return littleEndianToNative!uint(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
unittest
|
|
||||||
{
|
|
||||||
ubyte[] test0 = [0, 0, 0, 0];
|
|
||||||
ubyte[] test1 = [86, 75, 30, 9];
|
|
||||||
ubyte[] test2 = [255, 255, 255, 250];
|
|
||||||
assert(littleEndian(test0) == 0x00000000);
|
|
||||||
assert(littleEndian(test1) == 0x091e4b56);
|
|
||||||
assert(littleEndian(test2) == 0xfaffffff);
|
|
||||||
}
|
|
||||||
|
|
||||||
ubyte[] salsa20(in ubyte[] input)
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(input.length == 64);
|
assert(input.length == 64);
|
||||||
|
@ -332,7 +308,7 @@ enum ubyte[4] σ1 = [110, 100, 32, 51];
|
||||||
enum ubyte[4] σ2 = [ 50, 45, 98, 121];
|
enum ubyte[4] σ2 = [ 50, 45, 98, 121];
|
||||||
enum ubyte[4] σ3 = [116, 101, 32, 107];
|
enum ubyte[4] σ3 = [116, 101, 32, 107];
|
||||||
|
|
||||||
ubyte[] salsa20Exp(in ubyte[] key, in ubyte[] n)
|
ubyte[] salsa20Exp(in ubyte[] key, in ubyte[] n) @safe pure nothrow
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(key.length == 32);
|
assert(key.length == 32);
|
||||||
|
|
Loading…
Reference in a new issue