Changed test project type to WIndows 8.1

Changed test project framework from Nunit to MSTest
Changed HashAlgorithm from BouncyCastle to WinRT crypto
WIP progress bar in opendatabaseusercontrol
TextBox with button made generic
WIP implement copy on button click in Entry Page
This commit is contained in:
2017-11-06 19:01:01 +01:00
committed by BONNEVILLE Geoffroy
parent 53a54252e3
commit 8e690747e2
85 changed files with 2836 additions and 672 deletions

View File

@@ -1,17 +1,17 @@
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Cipher
{
[TestFixture]
[TestClass]
public class Chacha20Tests
{
[Test]
[TestMethod]
public void TestChacha20()
{
// ======================================================
@@ -42,7 +42,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
c.Seek(64, SeekOrigin.Begin); // Skip first block
c.Encrypt(pb, 0, pb.Length);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
}
#if DEBUG
@@ -80,7 +80,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
c.Encrypt(pb64, 0, pb64.Length); // Skip first block
c.Encrypt(pb, 0, pb.Length);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
}
// ======================================================
@@ -135,7 +135,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
byte[] pbEnc0 = msEnc.ToArray();
byte[] pbEnc = MemUtil.Mid(pbEnc0, 64, pbEnc0.Length - 64);
Assert.That(MemUtil.ArraysEqual(pbEnc, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pbEnc, pbExpc));
using (MemoryStream msCT = new MemoryStream(pbEnc0, false))
{
@@ -144,9 +144,9 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
{
byte[] pbPT = MemUtil.Read(cDec, pbEnc0.Length);
Assert.That(cDec.ReadByte(), Is.LessThan(0));
Assert.That(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 0, 64), pb64), Is.True);
Assert.That(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 64, pbEnc.Length), pb), Is.True);
Assert.IsTrue(cDec.ReadByte() < 0);
Assert.IsTrue(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 0, 64), pb64));
Assert.IsTrue(MemUtil.ArraysEqual(MemUtil.Mid(pbPT, 64, pbEnc.Length), pb));
}
}
}
@@ -196,7 +196,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
{
c.Decrypt(pb, 0, pb.Length);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
}
#endif
}

View File

@@ -1,22 +1,15 @@
using System;
using System.IO;
using System.Security;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Serialization;
using ModernKeePassLib.Utility;
#if KeePassLib
using KeePassLib.Cryptography.Cipher;
#else
using ModernKeePassLib.Cryptography.Cipher;
#endif
using NUnit.Framework;
using ModernKeePassLib.Utility;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
namespace ModernKeePassLib.Test.Cryptography.Cipher
{
[TestFixture()]
[TestClass()]
public class StandardAesEngineTests
{
// Test vector (official ECB test vector #356)
@@ -24,7 +17,7 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7
};
[Test]
[TestMethod]
public void TestEncryptStream()
{
byte[] pbIV = new byte[16];
@@ -36,13 +29,13 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var aes = new StandardAesEngine();
var inStream = aes.EncryptStream(outStream, pbTestKey, pbIV);
new BinaryWriter(inStream).Write(pbTestData);
//Assert.That(outStream.Position, Is.EqualTo(16));
Assert.AreEqual(outStream.Position, 16);
outStream.Position = 0;
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
Assert.That(outBytes, Is.EqualTo(pbReferenceCT));
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, pbReferenceCT));
}
[Test]
[TestMethod]
public void TestDecryptStream()
{
byte[] pbIV = new byte[16];
@@ -57,10 +50,10 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var aes = new StandardAesEngine();
var outStream = aes.DecryptStream(inStream, pbTestKey, pbIV);
var outBytes = new BinaryReaderEx(outStream, Encoding.UTF8, string.Empty).ReadBytes(16);
Assert.That(outBytes, Is.EqualTo(pbTestData));
Assert.IsTrue(MemUtil.ArraysEqual(outBytes, pbTestData));
}
[Test]
[TestMethod]
public void TestBouncyCastleAes()
{
byte[] pbIV = new byte[16];
@@ -75,10 +68,9 @@ namespace ModernKeePassLib.Test.Cryptography.Cipher
var aesEngine = new AesEngine();
//var parametersWithIv = new ParametersWithIV(new KeyParameter(pbTestKey), pbIV);
aesEngine.Init(true, new KeyParameter(pbTestKey));
Assert.That(aesEngine.GetBlockSize(), Is.EqualTo(pbTestData.Length));
Assert.AreEqual(aesEngine.GetBlockSize(), pbTestData.Length);
aesEngine.ProcessBlock(pbTestData, 0, pbTestData, 0);
//Assert.That(MemUtil.ArraysEqual(pbTestData, pbReferenceCT), Is.False);
Assert.That(pbTestData, Is.EqualTo(pbReferenceCT));
Assert.IsTrue(MemUtil.ArraysEqual(pbTestData, pbReferenceCT));
}
}
}

View File

@@ -1,60 +1,56 @@
using NUnit.Framework;
using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#if KeePassLib
using KeePassLib.Cryptography;
#else
using ModernKeePassLib.Cryptography;
#endif
using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Test.Cryptography
{
[TestFixture ()]
public class CryptoRandomStreamTests
{
void TestGetRandomBytes(CryptoRandomStream stream)
[TestClass]
public class CryptoRandomStreamTests
{
const uint length = 16;
var bytes1 = stream.GetRandomBytes (length);
Assert.That (bytes1.Length, Is.EqualTo (length));
var bytes2 = stream.GetRandomBytes (length);
Assert.That (bytes2, Is.Not.EqualTo (bytes1));
}
private void TestGetRandomBytes(CryptoRandomStream stream)
{
const uint length = 16;
var bytes1 = stream.GetRandomBytes(length);
Assert.AreEqual(bytes1.Length, (int)length);
var bytes2 = stream.GetRandomBytes(length);
Assert.IsFalse(MemUtil.ArraysEqual(bytes2, bytes1));
}
[Test ()]
public void TestGetRandomBytesCrsAlgorithmSalsa20 ()
{
var stream = new CryptoRandomStream (CrsAlgorithm.Salsa20, new byte[16]);
TestGetRandomBytes (stream);
}
[TestMethod]
public void TestGetRandomBytesCrsAlgorithmSalsa20()
{
var stream = new CryptoRandomStream(CrsAlgorithm.Salsa20, new byte[16]);
TestGetRandomBytes(stream);
}
[Test ()]
public void TestGetRandomBytesCrsAlgorithmArcFourVariant ()
{
var stream = new CryptoRandomStream (CrsAlgorithm.ArcFourVariant, new byte[16]);
TestGetRandomBytes (stream);
}
[TestMethod]
public void TestGetRandomBytesCrsAlgorithmArcFourVariant()
{
var stream = new CryptoRandomStream(CrsAlgorithm.ArcFourVariant, new byte[16]);
TestGetRandomBytes(stream);
}
void TestGetRandomInt64 (CryptoRandomStream stream)
{
var value1 = stream.GetRandomUInt64 ();
var value2 = stream.GetRandomUInt64 ();
Assert.That (value2, Is.Not.EqualTo (value1));
}
private void TestGetRandomInt64(CryptoRandomStream stream)
{
var value1 = stream.GetRandomUInt64();
var value2 = stream.GetRandomUInt64();
Assert.AreNotEqual(value2, value1);
}
[Test ()]
public void TestGetRandomInt64AlgorithmSalsa20 ()
{
var stream = new CryptoRandomStream (CrsAlgorithm.Salsa20, new byte[16]);
TestGetRandomInt64 (stream);
}
[TestMethod]
public void TestGetRandomInt64AlgorithmSalsa20()
{
var stream = new CryptoRandomStream(CrsAlgorithm.Salsa20, new byte[16]);
TestGetRandomInt64(stream);
}
[Test ()]
public void TestGetRandomInt64AlgorithmArcFourVariant ()
{
var stream = new CryptoRandomStream (CrsAlgorithm.ArcFourVariant, new byte[16]);
TestGetRandomInt64 (stream);
[TestMethod]
public void TestGetRandomInt64AlgorithmArcFourVariant()
{
var stream = new CryptoRandomStream(CrsAlgorithm.ArcFourVariant, new byte[16]);
TestGetRandomInt64(stream);
}
}
}
}

View File

@@ -1,44 +1,38 @@
using NUnit.Framework;
using System;
#if KeePassLib
using KeePassLib.Cryptography;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
#endif
namespace ModernKeePassLib.Test.Cryptography
{
[TestFixture ()]
public class CryptoRandomTests
{
[Test ()]
public void TestAddEntropy ()
[TestClass()]
public class CryptoRandomTests
{
// just making sure it does not throw an exception
CryptoRandom.Instance.AddEntropy (new byte[1]);
}
[TestMethod]
public void TestAddEntropy()
{
// just making sure it does not throw an exception
CryptoRandom.Instance.AddEntropy(new byte[1]);
}
[Test ()]
public void TestGetRandomBytes ()
{
const int length = 32;
var bytes1 = CryptoRandom.Instance.GetRandomBytes (length);
Assert.That (bytes1.Length, Is.EqualTo (length));
var bytes2 = CryptoRandom.Instance.GetRandomBytes (length);
Assert.That (bytes2, Is.Not.EqualTo (bytes1));
}
[TestMethod]
public void TestGetRandomBytes()
{
const int length = 32;
var bytes1 = CryptoRandom.Instance.GetRandomBytes(length);
Assert.AreEqual(bytes1.Length, length);
var bytes2 = CryptoRandom.Instance.GetRandomBytes(length);
Assert.AreNotEqual(bytes2, bytes1);
}
[Test ()]
public void TestGeneratedBytesCount ()
{
const int length = 1;
CryptoRandom.Instance.GetRandomBytes (length);
var count1 = CryptoRandom.Instance.GeneratedBytesCount;
CryptoRandom.Instance.GetRandomBytes (length);
var count2 = CryptoRandom.Instance.GeneratedBytesCount;
Assert.That (count2, Is.GreaterThan (count1));
[TestMethod]
public void TestGeneratedBytesCount()
{
const int length = 1;
CryptoRandom.Instance.GetRandomBytes(length);
var count1 = CryptoRandom.Instance.GeneratedBytesCount;
CryptoRandom.Instance.GetRandomBytes(length);
var count2 = CryptoRandom.Instance.GeneratedBytesCount;
Assert.IsTrue(count2 > count1);
}
}
}
}

View File

@@ -1,17 +1,17 @@
using System;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Hash
{
[TestFixture]
[TestClass]
public class Blake2bTests
{
[Test]
public void TestBlake2b()
[TestMethod]
public void TestBlake2bUtf8()
{
Blake2b h = new Blake2b();
@@ -19,7 +19,8 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
// From https://tools.ietf.org/html/rfc7693
byte[] pbData = StrUtil.Utf8.GetBytes("abc");
byte[] pbExpc = new byte[64] {
byte[] pbExpc = new byte[64]
{
0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D,
0x6A, 0x27, 0x97, 0xB6, 0x9F, 0x12, 0xF6, 0xE9,
0x4C, 0x21, 0x2F, 0x14, 0x68, 0x5A, 0xC4, 0xB7,
@@ -31,12 +32,18 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
};
byte[] pbC = h.ComputeHash(pbData);
Assert.That(MemUtil.ArraysEqual(pbC, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pbC, pbExpc));
}
[TestMethod]
public void TestBlake2bEmpty()
{
// ======================================================
// Computed using the official b2sum tool
Blake2b h = new Blake2b();
pbExpc = new byte[64] {
var pbExpc = new byte[64]
{
0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
@@ -47,18 +54,23 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
};
pbC = h.ComputeHash(MemUtil.EmptyByteArray);
Assert.That(MemUtil.ArraysEqual(pbC, pbExpc), Is.True);
var pbC = h.ComputeHash(MemUtil.EmptyByteArray);
Assert.IsTrue(MemUtil.ArraysEqual(pbC, pbExpc));
}
// ======================================================
[TestMethod]
public void TestBlake2bString()
{
// ======================================================
// Computed using the official b2sum tool
Blake2b h = new Blake2b();
string strS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;_-\r\n";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; ++i) sb.Append(strS);
pbData = StrUtil.Utf8.GetBytes(sb.ToString());
var pbData = StrUtil.Utf8.GetBytes(sb.ToString());
pbExpc = new byte[64] {
var pbExpc = new byte[64] {
0x59, 0x69, 0x8D, 0x3B, 0x83, 0xF4, 0x02, 0x4E,
0xD8, 0x99, 0x26, 0x0E, 0xF4, 0xE5, 0x9F, 0x20,
0xDC, 0x31, 0xEE, 0x5B, 0x45, 0xEA, 0xBB, 0xFC,
@@ -77,11 +89,11 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
h.TransformBlock(pbData, p, cb, pbData, p);
p += cb;
}
Assert.That(p, Is.EqualTo(pbData.Length));
Assert.AreEqual(p, pbData.Length);
h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
Assert.That(MemUtil.ArraysEqual(h.Hash, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(h.Hash, pbExpc));
h.Clear();
}

View File

@@ -1,14 +1,14 @@
using ModernKeePassLib.Cryptography;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Hash
{
[TestFixture]
[TestClass]
public class HmacTests
{
[Test]
[TestMethod]
public void TestHmac1()
{
// Test vectors from RFC 4231
@@ -26,7 +26,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
HmacEval(pbKey, pbMsg, pbExpc);
}
[Test]
[TestMethod]
public void TestHmac2()
{
var pbKey = new byte[131];
@@ -44,7 +44,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
HmacEval(pbKey, pbMsg, pbExpc);
}
[Test]
[TestMethod]
public void TestHmacSha1ComputeHash()
{
var expectedHash = "AC2C2E614882CE7158F69B7E3B12114465945D01";
@@ -52,11 +52,11 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
var key = StrUtil.Utf8.GetBytes("hello");
using (var result = new HMACSHA1(key))
{
Assert.That(ByteToString(result.ComputeHash(message)), Is.EqualTo(expectedHash));
Assert.AreEqual(ByteToString(result.ComputeHash(message)), expectedHash);
}
}
[Test]
[TestMethod]
public void TestHmacSha256ComputeHash()
{
var expectedHash = "09C1BD2DE4E5659C0EFAF9E6AE4723E9CF96B69609B4E562F6AFF1745D7BF4E0";
@@ -64,7 +64,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
var key = StrUtil.Utf8.GetBytes("hello");
using (var result = new HMACSHA256(key))
{
Assert.That(ByteToString(result.ComputeHash(message)), Is.EqualTo(expectedHash));
Assert.AreEqual(ByteToString(result.ComputeHash(message)), expectedHash);
}
}
@@ -79,7 +79,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
return (sbinary);
}
[Test]
[TestMethod]
public void TestHmacOtp()
{
var pbSecret = StrUtil.Utf8.GetBytes("12345678901234567890");
@@ -89,7 +89,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
for (var i = 0; i < vExp.Length; ++i)
{
Assert.That(HmacOtp.Generate(pbSecret, (ulong)i, 6, false, -1), Is.EqualTo(vExp[i]));
Assert.AreEqual(HmacOtp.Generate(pbSecret, (ulong)i, 6, false, -1), vExp[i]);
}
}
@@ -102,7 +102,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
byte[] pbHash = h.Hash;
Assert.That(MemUtil.ArraysEqual(pbHash, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pbHash, pbExpc));
// Reuse the object
h.Initialize();
@@ -110,7 +110,7 @@ namespace ModernKeePassLib.Test.Cryptography.Hash
h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
pbHash = h.Hash;
Assert.That(MemUtil.ArraysEqual(pbHash, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pbHash, pbExpc));
}
}
}

View File

@@ -1,31 +1,31 @@
using ModernKeePassLib.Cryptography.Hash;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.Hash
{
[TestFixture]
[TestClass]
public class SHAManagedTests
{
[Test]
[TestMethod]
public void TestSha256ComputeHash()
{
var expectedHash = "B822F1CD2DCFC685B47E83E3980289FD5D8E3FF3A82DEF24D7D1D68BB272EB32";
var message = StrUtil.Utf8.GetBytes("testing123");
using (var result = new SHA256Managed())
{
Assert.That(ByteToString(result.ComputeHash(message)), Is.EqualTo(expectedHash));
Assert.AreEqual(ByteToString(result.ComputeHash(message)), expectedHash);
}
}
[Test]
[TestMethod]
public void TestSha512ComputeHash()
{
var expectedHash = "4120117B3190BA5E24044732B0B09AA9ED50EB1567705ABCBFA78431A4E0A96B1152ED7F4925966B1C82325E186A8100E692E6D2FCB6702572765820D25C7E9E";
var message = StrUtil.Utf8.GetBytes("testing123");
using (var result = new SHA512Managed())
{
Assert.That(ByteToString(result.ComputeHash(message)), Is.EqualTo(expectedHash));
Assert.AreEqual(ByteToString(result.ComputeHash(message)), expectedHash);
}
}
private static string ByteToString(byte[] buff)

View File

@@ -1,71 +1,83 @@
using System.IO;
using NUnit.Framework;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Test.Cryptography
{
[TestFixture ()]
public class HashingStreamExTests
{
const string data = "test";
// The expected hash includes the \n added by WriteLine
static readonly byte[] sha256HashOfData = {
0xf2, 0xca, 0x1b, 0xb6, 0xc7, 0xe9, 0x07, 0xd0,
0x6d, 0xaf, 0xe4, 0x68, 0x7e, 0x57, 0x9f, 0xce,
0x76, 0xb3, 0x7e, 0x4e, 0x93, 0xb7, 0x60, 0x50,
0x22, 0xda, 0x52, 0xe6, 0xcc, 0xc2, 0x6f, 0xd2
};
[Test ()]
public void TestRead ()
[TestClass()]
public class HashingStreamExTests
{
// if we use larger size, StreamReader will read past newline and cause bad hash
var bytes = new byte[data.Length + 1];
using (var ms = new MemoryStream (bytes)) {
using (var sw = new StreamWriter (ms)) {
// set NewLine to ensure we don't run into cross-platform issues on Windows
sw.NewLine = "\n";
sw.WriteLine (data);
}
}
using (var ms = new MemoryStream (bytes)) {
using (var hs = new HashingStreamEx (ms, false, null)) {
using (var sr = new StreamReader (hs)) {
var read = sr.ReadLine ();
Assert.That (read, Is.EqualTo (data));
}
// When the StreamReader is disposed, it calls Dispose on the
//HasingStreamEx, which computes the hash.
Assert.That (hs.Hash, Is.EqualTo (sha256HashOfData));
}
}
}
const string data = "test";
[Test ()]
public void TestWrite ()
{
var bytes = new byte[16];
using (var ms = new MemoryStream (bytes)) {
using (var hs = new HashingStreamEx (ms, true, null)) {
using (var sw = new StreamWriter (hs)) {
// set NewLine to ensure we don't run into cross-platform issues on Windows
sw.NewLine = "\n";
sw.WriteLine (data);
}
// When the StreamWriter is disposed, it calls Dispose on the
//HasingStreamEx, which computes the hash.
Assert.That (hs.Hash, Is.EqualTo (sha256HashOfData));
// The expected hash includes the \n added by WriteLine
static readonly byte[] sha256HashOfData =
{
0xf2, 0xca, 0x1b, 0xb6, 0xc7, 0xe9, 0x07, 0xd0,
0x6d, 0xaf, 0xe4, 0x68, 0x7e, 0x57, 0x9f, 0xce,
0x76, 0xb3, 0x7e, 0x4e, 0x93, 0xb7, 0x60, 0x50,
0x22, 0xda, 0x52, 0xe6, 0xcc, 0xc2, 0x6f, 0xd2
};
[TestMethod]
public void TestRead()
{
// if we use larger size, StreamReader will read past newline and cause bad hash
var bytes = new byte[data.Length + 1];
using (var ms = new MemoryStream(bytes))
{
using (var sw = new StreamWriter(ms))
{
// set NewLine to ensure we don't run into cross-platform issues on Windows
sw.NewLine = "\n";
sw.WriteLine(data);
}
}
using (var ms = new MemoryStream(bytes))
{
using (var hs = new HashingStreamEx(ms, false, null))
{
using (var sr = new StreamReader(hs))
{
var read = sr.ReadLine();
Assert.AreEqual(read, data);
}
// When the StreamReader is disposed, it calls Dispose on the
//HasingStreamEx, which computes the hash.
Assert.IsTrue(MemUtil.ArraysEqual(hs.Hash, sha256HashOfData));
}
}
}
}
using (var ms = new MemoryStream (bytes)) {
using (var sr = new StreamReader (ms)) {
var read = sr.ReadLine ();
Assert.That (read, Is.EqualTo (data));
[TestMethod]
public void TestWrite()
{
var bytes = new byte[16];
using (var ms = new MemoryStream(bytes))
{
using (var hs = new HashingStreamEx(ms, true, null))
{
using (var sw = new StreamWriter(hs))
{
// set NewLine to ensure we don't run into cross-platform issues on Windows
sw.NewLine = "\n";
sw.WriteLine(data);
}
// When the StreamWriter is disposed, it calls Dispose on the
//HasingStreamEx, which computes the hash.
Assert.IsTrue(MemUtil.ArraysEqual(hs.Hash, sha256HashOfData));
}
}
using (var ms = new MemoryStream(bytes))
{
using (var sr = new StreamReader(ms))
{
var read = sr.ReadLine();
Assert.AreEqual(read, data);
}
}
}
}
}
}
}

View File

@@ -1,36 +1,32 @@
using NUnit.Framework;
using System;
using System.Text;
#if KeePassLib
using KeePassLib.Cryptography;
#else
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
#endif
namespace ModernKeePassLib.Test.Cryptography
{
[TestFixture ()]
public class HmacOtpTests
{
// Using the test case from Appendix D of RFC 4226
const string secret = "12345678901234567890";
static readonly string[] expectedHOTP = new string[] {
"755224", "287082", "359152", "969429", "338314",
"254676", "287922", "162583", "399871", "520489"
};
[Test ()]
public void TestGenerate ()
[TestClass()]
public class HmacOtpTests
{
var secretBytes = Encoding.UTF8.GetBytes (secret);
// Using the test case from Appendix D of RFC 4226
for (ulong i = 0; i < 10; i++) {
var hotp = HmacOtp.Generate (secretBytes, i, 6, false, -1);
Assert.That (hotp, Is.EqualTo (expectedHOTP[i]));
}
const string secret = "12345678901234567890";
static readonly string[] expectedHOTP = new string[]
{
"755224", "287082", "359152", "969429", "338314",
"254676", "287922", "162583", "399871", "520489"
};
[TestMethod]
public void TestGenerate()
{
var secretBytes = Encoding.UTF8.GetBytes(secret);
for (ulong i = 0; i < 10; i++)
{
var hotp = HmacOtp.Generate(secretBytes, i, 6, false, -1);
Assert.AreEqual(hotp, expectedHOTP[i]);
}
}
}
}
}

View File

@@ -1,15 +1,15 @@
using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
{
[TestFixture]
[TestClass]
public class AesKdfTests
{
[Test]
[TestMethod]
public void TestAesKdf()
{
// Up to KeePass 2.34, the OtpKeyProv plugin used the public
@@ -27,7 +27,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
var pbMan = new byte[pbKey.Length];
Array.Copy(pbKey, pbMan, pbKey.Length);
Assert.That(AesKdf.TransformKeyManaged(pbMan, pbSeed, uRounds), Is.True);
Assert.IsTrue(AesKdf.TransformKeyManaged(pbMan, pbSeed, uRounds));
pbMan = CryptoUtil.HashSha256(pbMan);
var kdf = new AesKdf();
@@ -36,7 +36,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
p.SetByteArray(AesKdf.ParamSeed, pbSeed);
var pbKdf = kdf.Transform(pbKey, p);
Assert.That(MemUtil.ArraysEqual(pbMan, pbKdf), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pbMan, pbKdf));
}
}
}

View File

@@ -1,13 +1,13 @@
using ModernKeePassLib.Cryptography.KeyDerivation;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
{
[TestFixture]
[TestClass]
public class Argon2Tests
{
[Test]
[TestMethod]
public void TestArgon2()
{
Argon2Kdf kdf = new Argon2Kdf();
@@ -20,7 +20,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
var p = kdf.GetDefaultParameters();
kdf.Randomize(p);
Assert.That(p.GetUInt32(Argon2Kdf.ParamVersion, 0), Is.EqualTo(0x13U));
Assert.AreEqual(p.GetUInt32(Argon2Kdf.ParamVersion, 0), 0x13U);
byte[] pbMsg = new byte[32];
for (int i = 0; i < pbMsg.Length; ++i) pbMsg[i] = 1;
@@ -50,7 +50,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
byte[] pb = kdf.Transform(pbMsg, p);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
// ======================================================
// From the official Argon2 1.3 reference code package
@@ -67,7 +67,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
// ======================================================
// From the official 'phc-winner-argon2-20151206.zip'
@@ -84,7 +84,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
// ======================================================
// Computed using the official 'argon2' application
@@ -110,7 +110,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 10) * 1024); // 1 MB
p.SetUInt64(Argon2Kdf.ParamIterations, 3);
@@ -124,9 +124,10 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));
p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 20) * 1024); // 1 GB
// TODO: Out of memory exception
/*p.SetUInt64(Argon2Kdf.ParamMemory, (1 << 20) * 1024); // 1 GB
p.SetUInt64(Argon2Kdf.ParamIterations, 2);
p.SetUInt32(Argon2Kdf.ParamParallelism, 3);
@@ -139,7 +140,7 @@ namespace ModernKeePassLib.Test.Cryptography.KeyDerivation
pb = kdf.Transform(pbMsg, p);
Assert.That(MemUtil.ArraysEqual(pb, pbExpc), Is.True);
Assert.IsTrue(MemUtil.ArraysEqual(pb, pbExpc));*/
}
}
}