Formating fixes

This commit is contained in:
Johannes Loher 2016-02-28 14:25:58 +01:00
parent 753a0cd366
commit 8c203bd412
6 changed files with 76 additions and 73 deletions

View file

@ -1,7 +1,7 @@
module actions;
import std.stdio;
import std.base64: Base64;
import std.stdio : File, stdin, stdout, writeln;
import std.base64 : Base64;
import std.random : Random, uniform;
import std.algorithm : joiner;
@ -17,24 +17,24 @@ void encrypt(string keyFileName, Cipher cipher, bool armor)
{
auto key = loadKey(keyFileName, armor);
ubyte[8] nonce;
if(armor)
if (armor)
{
ubyte[] buf;
foreach(b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
{
buf ~= [b];
if(buf.length == 57)
if (buf.length == 57)
{
stdout.writeln(Base64.encode(buf));
buf = [];
}
}
if(buf !is null)
if (buf !is null)
stdout.writeln(Base64.encode(buf));
}
else
{
foreach(b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
stdout.rawWrite([b]);
}
}
@ -43,17 +43,17 @@ void decrypt(string keyFileName, Cipher cipher, bool armor)
{
auto key = loadKey(keyFileName, armor);
ubyte[8] nonce;
if(armor)
if (armor)
{
ubyte[] buf;
foreach(b; Base64.decoder(stdin.byLine).joiner.cipherFunction(key, nonce, cipher))
foreach (b; Base64.decoder(stdin.byLine).joiner.cipherFunction(key, nonce, cipher))
{
stdout.rawWrite([b]);
}
}
else
{
foreach(b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
stdout.rawWrite([b]);
}
}
@ -62,7 +62,8 @@ void generateKey(bool armor)
{
auto rng = Random();
auto randomDevice = File(randomDeviceName, "r");
scope(exit) randomDevice.close();
scope (exit)
randomDevice.close();
uint[1] seed;
randomDevice.rawRead(seed);
rng.seed(seed[0]);
@ -71,7 +72,7 @@ void generateKey(bool armor)
foreach (ref b; key)
b = uniform!ubyte(rng);
if(armor)
if (armor)
{
writeln(Base64.encode(key));
}
@ -86,11 +87,13 @@ private:
ubyte[32] loadKey(string filename, bool armor)
{
auto keyFile = File(filename, "r");
scope (exit)
keyFile.close();
ubyte[32] key;
if(armor)
if (armor)
{
ubyte[] tempKey;
foreach(line; keyFile.byLine)
foreach (line; keyFile.byLine)
tempKey ~= Base64.decode(line);
key = tempKey;
}

View file

@ -1,15 +1,13 @@
import std.getopt;
import std.string : format;
import std.stdio: stderr, writeln;
import std.stdio : stderr, writeln;
import cipher : Cipher;
import actions;
int main(string[] args)
{
bool[string] actions = [ "genKey" : false,
"encrypt" : false,
"decrypt" : false ];
bool[string] actions = ["genKey" : false, "encrypt" : false, "decrypt" : false];
Cipher cipher = Cipher.chacha20;
string keyFileName = "symkey.asc";
@ -18,8 +16,7 @@ int main(string[] args)
GetoptResult result;
try
{
result = getopt(
args,
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"],
@ -28,34 +25,34 @@ int main(string[] args)
"key|k", "The file which contains the key (default: %s).".format(keyFileName), &keyFileName,
"armor|a", "use ascii-armored I/O.", &armor);
}
catch(Exception e)
catch (Exception e)
{
stderr.writeln(e.msg);
return 1;
}
size_t numberOfActions;
foreach(value; actions.values)
foreach (value; actions.values)
numberOfActions += value;
if(numberOfActions == 1)
if (numberOfActions == 1)
{
try
{
if(actions["genKey"])
if (actions["genKey"])
{
generateKey(armor);
}
else if(actions["encrypt"])
else if (actions["encrypt"])
{
encrypt(keyFileName, cipher, armor);
}
else if(actions["decrypt"])
else if (actions["decrypt"])
{
decrypt(keyFileName, cipher, armor);
}
}
catch(Exception e)
catch (Exception e)
{
stderr.writeln(e.msg);
return 1;
@ -66,7 +63,7 @@ int main(string[] args)
result.helpWanted = true;
}
if(result.helpWanted)
if (result.helpWanted)
printHelp(result.options);
return 0;

View file

@ -4,7 +4,7 @@ private import std.traits : isUnsigned;
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative;
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);
// TODO: ensure the compiler does not create different code paths here
@ -21,7 +21,7 @@ unittest
}
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);
// TODO: ensure the compiler does not create different code paths here

View file

@ -3,7 +3,6 @@ module chacha20;
private import std.string : format;
private import std.range : isInputRange, isForwardRange, ElementType;
private import std.array;
private import std.traits : hasElaborateCopyConstructor;
private import bitmanip;
@ -12,7 +11,7 @@ public:
// TODO: Check unittests (Use reliable software to check if the results are correct)
auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if(isInputRange!R && is(ElementType!R : ubyte))
if (isInputRange!R && is(ElementType!R : ubyte))
{
static struct rangeResult
{
@ -47,7 +46,7 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
{
assert(!empty);
chachaSection.popFront();
if(chachaSection.empty)
if (chachaSection.empty)
{
++count;
chachaSection = chacha20Exp(key, nonce ~ littleEndianInv(count));
@ -223,8 +222,8 @@ out(result)
}
body
{
auto x00 = littleEndian(input[0..4]), x01 = littleEndian(input[4..8]),
x02 = littleEndian(input[8..12]), x03 = littleEndian(input[12..16]),
auto x00 = littleEndian(input[00..04]), x01 = littleEndian(input[04..08]),
x02 = littleEndian(input[08..12]), x03 = littleEndian(input[12..16]),
x04 = littleEndian(input[16..20]), x05 = littleEndian(input[20..24]),
x06 = littleEndian(input[24..28]), x07 = littleEndian(input[28..32]),
x08 = littleEndian(input[32..36]), x09 = littleEndian(input[36..40]),
@ -237,7 +236,7 @@ body
y08 = x08, y09 = x09, y10 = x10, y11 = x11,
y12 = x12, y13 = x13, y14 = x14, y15 = x15;
foreach(i; 0..10)
foreach (i; 0..10)
{
mixin(colRound!(x00, x01, x02, x03, x04, x05, x06, x07,
x08, x09, x10, x11, x12, x13, x14, x15));
@ -280,7 +279,7 @@ unittest
75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95,
225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108,
94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186];
foreach(i; 0..1000000)
foreach (i; 0..1000000)
test3 = chacha20(test3);
assert(test0 == [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -330,12 +329,12 @@ unittest
ubyte[] n;
key.length = 32;
n.length = 16;
foreach(i; 0..16)
foreach (i; 0..16)
key[i] = cast(ubyte)(i + 1);
foreach(i; 16..32)
foreach (i; 16..32)
key[i] = cast(ubyte)(i + 200 - 15);
foreach(i; 0..16)
foreach (i; 0..16)
n[i] = cast(ubyte)(i + 1+ 100);
assert(chacha20Exp(key, n) == [ 2, 7, 55,183,240,232, 0,145,207,208,120,131,146, 9,130, 31,

View file

@ -1,6 +1,8 @@
module cipher;
private import std.range : isInputRange, ElementType, InputRange, ForwardRange, inputRangeObject;
private import std.range : isInputRange, isForwardRange, ElementType, InputRange, ForwardRange, inputRangeObject;
private import std.string : format;
private import salsa20;
private import chacha20;
@ -12,8 +14,11 @@ enum Cipher
chacha20
}
mixin(cipherFunctionString.format(q{InputRange}, q{isInputRange!R && !(isForwardRange!R)}));
mixin(cipherFunctionString.format(q{ForwardRange}, q{isForwardRange!R}));
mixin(cipherFunctionString.format(q{InputRange},
q{isInputRange!R && !(isForwardRange!R)}));
mixin(cipherFunctionString.format(q{ForwardRange},
q{isForwardRange!R}));
unittest
{
@ -34,9 +39,9 @@ private:
enum cipherFunctionString = q{
%s!(ElementType!R) cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
if(is(ElementType!R : ubyte) && %s)
if (is(ElementType!R : ubyte) && %s)
{
final switch(cipher)
final switch (cipher)
{
case Cipher.salsa20:
return range.salsa20Cipher(key, nonce).inputRangeObject;

View file

@ -3,14 +3,13 @@ module salsa20;
private import std.string : format;
private import std.range : isInputRange, isForwardRange, ElementType;
private import std.array;
private import std.traits : hasElaborateCopyConstructor;
private import bitmanip;
public:
auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if(isInputRange!R && is(ElementType!R : ubyte))
if (isInputRange!R && is(ElementType!R : ubyte))
{
static struct rangeResult
{
@ -45,7 +44,7 @@ auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
{
assert(!empty);
salsaSection.popFront();
if(salsaSection.empty)
if (salsaSection.empty)
{
++count;
salsaSection = salsa20Exp(key, nonce ~ littleEndianInv(count));
@ -223,8 +222,8 @@ out(result)
}
body
{
auto x00 = littleEndian(input[0..4]), x01 = littleEndian(input[4..8]),
x02 = littleEndian(input[8..12]), x03 = littleEndian(input[12..16]),
auto x00 = littleEndian(input[00..04]), x01 = littleEndian(input[04..08]),
x02 = littleEndian(input[08..12]), x03 = littleEndian(input[12..16]),
x04 = littleEndian(input[16..20]), x05 = littleEndian(input[20..24]),
x06 = littleEndian(input[24..28]), x07 = littleEndian(input[28..32]),
x08 = littleEndian(input[32..36]), x09 = littleEndian(input[36..40]),
@ -237,7 +236,7 @@ body
y08 = x08, y09 = x09, y10 = x10, y11 = x11,
y12 = x12, y13 = x13, y14 = x14, y15 = x15;
foreach(i; 0..10)
foreach (i; 0..10)
{
mixin(colRound!(x00, x01, x02, x03, x04, x05, x06, x07,
x08, x09, x10, x11, x12, x13, x14, x15));
@ -280,7 +279,7 @@ unittest
75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95,
225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108,
94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186];
foreach(i; 0..1000000)
foreach (i; 0..1000000)
test3 = salsa20(test3);
assert(test0 == [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -330,12 +329,12 @@ unittest
ubyte[] n;
key.length = 32;
n.length = 16;
foreach(i; 0..16)
foreach (i; 0..16)
key[i] = cast(ubyte)(i + 1);
foreach(i; 16..32)
foreach (i; 16..32)
key[i] = cast(ubyte)(i + 200 - 15);
foreach(i; 0..16)
foreach (i; 0..16)
n[i] = cast(ubyte)(i + 1+ 100);
assert(salsa20Exp(key, n) == [ 69, 37, 68, 39, 41, 15,107,193,255,139,122, 6,170,233,217, 98,