2016-02-14 11:00:28 +01:00
|
|
|
module actions;
|
|
|
|
|
2016-02-28 14:25:58 +01:00
|
|
|
import std.stdio : File, stdin, stdout, writeln;
|
|
|
|
import std.base64 : Base64;
|
2016-02-14 11:00:28 +01:00
|
|
|
import std.random : Random, uniform;
|
|
|
|
import std.algorithm : joiner;
|
|
|
|
|
|
|
|
import cipher;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum chunkSize = 4096;
|
|
|
|
|
|
|
|
enum string randomDeviceName = "/dev/random";
|
|
|
|
|
|
|
|
void encrypt(string keyFileName, Cipher cipher, bool armor)
|
|
|
|
{
|
|
|
|
auto key = loadKey(keyFileName, armor);
|
|
|
|
ubyte[8] nonce;
|
2016-02-28 14:25:58 +01:00
|
|
|
if (armor)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
ubyte[] buf;
|
2016-02-28 14:25:58 +01:00
|
|
|
foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
buf ~= [b];
|
2016-02-28 14:25:58 +01:00
|
|
|
if (buf.length == 57)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
stdout.writeln(Base64.encode(buf));
|
|
|
|
buf = [];
|
|
|
|
}
|
|
|
|
}
|
2016-02-28 14:25:58 +01:00
|
|
|
if (buf !is null)
|
2016-02-14 11:00:28 +01:00
|
|
|
stdout.writeln(Base64.encode(buf));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-28 14:25:58 +01:00
|
|
|
foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
|
2016-02-14 11:00:28 +01:00
|
|
|
stdout.rawWrite([b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void decrypt(string keyFileName, Cipher cipher, bool armor)
|
|
|
|
{
|
|
|
|
auto key = loadKey(keyFileName, armor);
|
|
|
|
ubyte[8] nonce;
|
2016-02-28 14:25:58 +01:00
|
|
|
if (armor)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
ubyte[] buf;
|
2016-02-28 14:25:58 +01:00
|
|
|
foreach (b; Base64.decoder(stdin.byLine).joiner.cipherFunction(key, nonce, cipher))
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
stdout.rawWrite([b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-28 14:25:58 +01:00
|
|
|
foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
|
2016-02-14 11:00:28 +01:00
|
|
|
stdout.rawWrite([b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void generateKey(bool armor)
|
|
|
|
{
|
|
|
|
auto rng = Random();
|
|
|
|
auto randomDevice = File(randomDeviceName, "r");
|
2016-02-28 14:25:58 +01:00
|
|
|
scope (exit)
|
|
|
|
randomDevice.close();
|
2016-02-14 11:00:28 +01:00
|
|
|
uint[1] seed;
|
|
|
|
randomDevice.rawRead(seed);
|
|
|
|
rng.seed(seed[0]);
|
|
|
|
|
|
|
|
ubyte[32] key;
|
|
|
|
foreach (ref b; key)
|
|
|
|
b = uniform!ubyte(rng);
|
|
|
|
|
2016-02-28 14:25:58 +01:00
|
|
|
if (armor)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
writeln(Base64.encode(key));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stdout.rawWrite(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ubyte[32] loadKey(string filename, bool armor)
|
|
|
|
{
|
|
|
|
auto keyFile = File(filename, "r");
|
2016-02-28 14:25:58 +01:00
|
|
|
scope (exit)
|
|
|
|
keyFile.close();
|
2016-02-14 11:00:28 +01:00
|
|
|
ubyte[32] key;
|
2016-02-28 14:25:58 +01:00
|
|
|
if (armor)
|
2016-02-14 11:00:28 +01:00
|
|
|
{
|
|
|
|
ubyte[] tempKey;
|
2016-02-28 14:25:58 +01:00
|
|
|
foreach (line; keyFile.byLine)
|
2016-02-14 11:00:28 +01:00
|
|
|
tempKey ~= Base64.decode(line);
|
|
|
|
key = tempKey;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keyFile.rawRead(key);
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|