added testdecrypt

This commit is contained in:
Kevin Froman 2020-12-20 09:41:14 +00:00
parent 0d2fa7e380
commit 8a74b57724
2 changed files with 16 additions and 0 deletions

View File

@ -24,8 +24,10 @@ namespace rinseoffcli
stderrStream.Flush(); stderrStream.Flush();
} }
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
byte[] plaintext = {}; byte[] plaintext = {};
byte[] readBytes(string file){ byte[] readBytes(string file){
// Read bytes in from a file, exit with error message if not possible
byte[] bytesToRead = {}; byte[] bytesToRead = {};
try{ try{
bytesToRead = File.ReadAllBytes(file); bytesToRead = File.ReadAllBytes(file);
@ -46,6 +48,7 @@ namespace rinseoffcli
} }
var stdout = Console.OpenStandardOutput(); var stdout = Console.OpenStandardOutput();
try{ try{
// Decrypt a file using a key file
plaintext = RinseOff.decrypt_secret_bytes( plaintext = RinseOff.decrypt_secret_bytes(
readBytes(filepath), readBytes(filepath),
readBytes(keypath) readBytes(keypath)
@ -59,6 +62,7 @@ namespace rinseoffcli
stderrWrite("Could not decrypt " + filepath + " with " + keypath); stderrWrite("Could not decrypt " + filepath + " with " + keypath);
Environment.Exit(12); Environment.Exit(12);
} }
// print the plaintext and exit
foreach(byte b in plaintext){ foreach(byte b in plaintext){
stdout.WriteByte(b); stdout.WriteByte(b);
} }

View File

@ -1,6 +1,7 @@
using NUnit.Framework; using NUnit.Framework;
using System.Text; using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using Sodium;
using rinseoff; using rinseoff;
namespace testDecrypted namespace testDecrypted
@ -15,7 +16,18 @@ namespace testDecrypted
[Test] [Test]
public void testDecrypted() public void testDecrypted()
{ {
var key = Sodium.SecretBox.GenerateKey();
var nonce = Sodium.SecretBox.GenerateNonce();
var msg = Encoding.Default.GetBytes("hey");
var encrypted = Sodium.SecretBox.Create(msg, nonce, key);
var combined = new List<byte>();
combined.AddRange(nonce);
combined.AddRange(encrypted);
Assert.AreEqual(
RinseOff.decrypt_secret_bytes(combined.ToArray(), key),
msg
);
} }
} }
} }