added entropy calculator

This commit is contained in:
Kevin Froman 2020-05-25 00:05:49 -05:00
parent a96285b3a4
commit 1aeb224be8
6 changed files with 116 additions and 0 deletions

View File

@ -2,6 +2,7 @@ using System.IO;
using System; using System;
using treasurechest.STDIOWrapper; using treasurechest.STDIOWrapper;
using getpass; using getpass;
using ShannonEntropyCal;
namespace treasurechestCLI{ namespace treasurechestCLI{
@ -11,6 +12,7 @@ namespace treasurechestCLI{
int counter = 1; int counter = 1;
string message; string message;
string passphrase; string passphrase;
string encrypted;
translations.Strings strings = new translations.Strings(); translations.Strings strings = new translations.Strings();
@ -52,6 +54,7 @@ namespace treasurechestCLI{
continue; continue;
} }
passphrase = GetPass.getPass(strings.PASSPHRASE); passphrase = GetPass.getPass(strings.PASSPHRASE);
encrypted =
} }
else if (choice == encryptMenuOptions.Length){ else if (choice == encryptMenuOptions.Length){
break; break;

View File

31
tests/testEntropy.cs Normal file
View File

@ -0,0 +1,31 @@
using NUnit.Framework;
using ShannonEntropyCal;
using System;
namespace entropytests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestShannonEntropyLow()
{
string low = "abc123";
if (EntropyCal.EntropyValue(low) > 3.0){
Assert.Fail();
}
}
[Test]
public void TestShannonEntropyHigh()
{
string high = "ý¼¸²>æ{£¤@TçKA¥£åKPk.rPoSo}fÑú½§rêÆÀðke(9/¹©ÔRqTãîý`Çóè°T²þµ)ÁÄÒÙr7éijÈ·Ñø{.8'ü*=Å.ôþSø&ÏßP9D}\"û+îÏæ¼aZ-'ûÐмÊZh5³ÒD®/YÙ¤(a·]Ðf";
Assert.IsTrue(EntropyCal.EntropyValue(high) > 6.3);
}
}
}

View File

@ -0,0 +1,37 @@
// Taken from https://github.com/Kaynn-Cahya/Shannon-Entropy with no license given.
// Since this was published on nuget, i am assuming the author is ok with it being used in open source
using System;
using System.Collections.Generic;
using System.Linq;
// Test: testEntropy.cs
namespace ShannonEntropyCal
{
public class EntropyCal
{
public static double EntropyValue(string message)
{
Dictionary<char, int> K = message.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count());
double EntropyValue = 0;
foreach (var character in K)
{
double PR = character.Value / (double) message.Length;
EntropyValue -= PR * Math.Log(PR, 2);
}
return EntropyValue;
}
public static double EntropyBits(string message)
{
Dictionary<char, int> K = message.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count());
double EntropyValue = 0;
foreach (var character in K)
{
double PR = character.Value / (double) message.Length;
EntropyValue -= PR * Math.Log(PR, 2);
}
return Math.Ceiling(EntropyValue) * message.Length;
}
}
}

View File

@ -0,0 +1,26 @@
using System.Text;
using Sodium;
namespace chestcrypto{
namespace kdf{
public class DeterministicSymmetricKey{
// Test
public static byte[] generate(string passphrase, bool extraSensitive=false){
var nonce = SecretBox.GenerateNonce();
int strength = 2;
if (extraSensitive){
strength = 3;
}
return PasswordHash.ArgonHashBinary(Encoding.UTF8.GetBytes(passphrase), // Passphrase converted to bytes
PasswordHash.ArgonGenerateSalt(), // Salt
strength, strength,
32);
}
}
}
}

View File

@ -0,0 +1,19 @@
using Sodium;
using System;
using chestcrypto.kdf;
namespace treasurechest{
namespace symmetric{
public class EncryptWithPassphrase{
/* Class name is somewhat misleading as we actually derive a key from a string pass and use the key for secret key crypto*/
public static byte[] encrypt(byte[] data, string passphrase, bool extraSensitive = false){
byte[] key = DeterministicSymmetricKey.generate(passphrase, extraSensitive);
return SecretBox.Create(data, SecretBox.GenerateNonce(), key);
}
}
}
}