option to read from stdin when loading data

This commit is contained in:
Kevin Froman 2020-12-30 07:15:33 +00:00
parent 175041ebe5
commit 369b23811b
2 changed files with 28 additions and 17 deletions

View File

@ -41,6 +41,7 @@ Then input the data to store through stdin.
`$ rinseoffcli load /path/to/stored/data /path/to/key` `$ rinseoffcli load /path/to/stored/data /path/to/key`
If the key is valid, the plaintext will be outputted through stdout. If the key is valid, the plaintext will be outputted through stdout.
if data path is "stdin" it will be read from pipe according
## Securely erase data ## Securely erase data

View File

@ -24,7 +24,7 @@ namespace rinseoffcli
{ {
class Program class Program
{ {
public static string version = "1.0.0"; public static string version = "2.0.0";
enum ErrorCode : byte enum ErrorCode : byte
{ {
// Exit error codes indexed from 1 // Exit error codes indexed from 1
@ -59,9 +59,26 @@ namespace rinseoffcli
stderrStream.WriteLine(msg); stderrStream.WriteLine(msg);
stderrStream.Flush(); stderrStream.Flush();
} }
static byte[] readUntilClose(){
// Read binary from STDIN until close
var readData = new List<byte>();
Stream inputStream = Console.OpenStandardInput();
int inp;
while(true){
inp = inputStream.ReadByte();
if (inp != -1){
readData.Add((byte) inp);
}
else{
return readData.ToArray();
}
}
}
static void loadData(string filepath, string keypath){ static void loadData(string filepath, string keypath){
// Load in an encrypted file and use a key file to decrypt it, then log bytes back to stdout // Load in an encrypted file and use a key file to decrypt it, then log bytes back to stdout
byte[] plaintext = {}; byte[] plaintext = {};
byte[] ciphertext = {};
byte[] readBytes(string file){ byte[] readBytes(string file){
// Read bytes in from a file, exit with error message if not possible // Read bytes in from a file, exit with error message if not possible
byte[] bytesToRead = {}; byte[] bytesToRead = {};
@ -83,10 +100,17 @@ namespace rinseoffcli
return bytesToRead; return bytesToRead;
} }
var stdout = Console.OpenStandardOutput(); var stdout = Console.OpenStandardOutput();
if (filepath.Equals("stdin")){
ciphertext = readUntilClose();
}
else{
ciphertext = readBytes(filepath);
}
try{ try{
// Decrypt a file using a key file // Decrypt a file using a key file
plaintext = RinseOff.decrypt_secret_bytes( plaintext = RinseOff.decrypt_secret_bytes(
readBytes(filepath), ciphertext,
readBytes(keypath) readBytes(keypath)
); );
} }
@ -98,6 +122,7 @@ namespace rinseoffcli
stderrWrite("Could not decrypt " + filepath + " with " + keypath); stderrWrite("Could not decrypt " + filepath + " with " + keypath);
Environment.Exit((int)ErrorCode.InvalidKeyFile); Environment.Exit((int)ErrorCode.InvalidKeyFile);
} }
ciphertext = null;
// print the plaintext and exit // print the plaintext and exit
foreach(byte b in plaintext){ foreach(byte b in plaintext){
stdout.WriteByte(b); stdout.WriteByte(b);
@ -116,21 +141,6 @@ namespace rinseoffcli
stdout.Flush(); stdout.Flush();
} }
byte[] readUntilClose(){
// Read binary from STDIN until close
var readData = new List<byte>();
Stream inputStream = Console.OpenStandardInput();
int inp;
while(true){
inp = inputStream.ReadByte();
if (inp != -1){
readData.Add((byte) inp);
}
else{
return readData.ToArray();
}
}
}
// Encrypt stdin with keyfile data then write out to output file // Encrypt stdin with keyfile data then write out to output file
try{ try{
encryptedInput = RinseOff.encrypt_secret_bytes(readUntilClose(), File.ReadAllBytes(keypath)); encryptedInput = RinseOff.encrypt_secret_bytes(readUntilClose(), File.ReadAllBytes(keypath));