learncrypto/source/cipher.d
2016-08-01 01:42:29 +02:00

52 lines
1.2 KiB
D

module cipher;
private:
import std.range : isInputRange, isForwardRange, ElementType, InputRange,
ForwardRange, inputRangeObject;
import std.string : format;
import salsa20;
import chacha20;
public:
enum Cipher
{
salsa20,
chacha20
}
template cipherFunction(R) if (isInputRange!R && is(ElementType!R : ubyte))
{
static if (isForwardRange!R)
alias ReturnType = ForwardRange;
else
alias ReturnType = InputRange;
ReturnType!(ElementType!R) cipherFunction(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
{
final switch (cipher)
{
case Cipher.salsa20:
return range.salsa20Cipher(key, nonce).inputRangeObject;
case Cipher.chacha20:
return range.chacha20Cipher(key, nonce).inputRangeObject;
}
}
}
unittest
{
import std.array;
immutable ubyte[32] key;
immutable ubyte[8] nonce;
ubyte[] a = [1, 2, 3];
auto b = a.cipherFunction(key, nonce, Cipher.salsa20);
InputRange!ubyte c = a.inputRangeObject;
auto d = c.cipherFunction(key, nonce, Cipher.salsa20);
static assert(isForwardRange!(typeof(b)));
static assert(isInputRange!(typeof(d)) && !(isForwardRange!(typeof(d))));
}