public void TestPerfKeyGen() { int iter = 100; Salt salt = new Salt(); byte[] passbytes = Password.Encoding.GetBytes(TEST_PASSWORD); IPasswordDerivedBytes[] types = new IPasswordDerivedBytes[] { new PBKDF2(passbytes, salt, iter), new HashDerivedBytes <HMACMD5>(passbytes, salt, iter), new HashDerivedBytes <HMACSHA1>(passbytes, salt, iter), new HashDerivedBytes <HMACSHA256>(passbytes, salt, iter), new HashDerivedBytes <HMACSHA384>(passbytes, salt, iter), new HashDerivedBytes <HMACSHA512>(passbytes, salt, iter), }; foreach (IPasswordDerivedBytes db in types) { byte[] key; Stopwatch w = new Stopwatch(); w.Start(); for (int i = 0; i < 100; i++) { key = new PasswordKey(db, salt).CreateKey().Key; } w.Stop(); Console.Error.WriteLine("{0,10} {1}", w.ElapsedMilliseconds, db.GetType().Name); } }
public void TestReset() { using (IPasswordDerivedBytes pd = DerivedBytes(TEST_PASSWORD)) { pd.IterationCount = 10; byte[] bytes = pd.GetBytes(1000); Assert.AreNotEqual(bytes, pd.GetBytes(bytes.Length)); pd.Reset(); Assert.AreEqual(bytes, pd.GetBytes(bytes.Length)); } }
public void TestIteration() { using (IPasswordDerivedBytes pd = DerivedBytes(TEST_PASSWORD)) { byte[] bytes = pd.GetBytes(4); pd.IterationCount *= 2; Assert.AreNotEqual(bytes, pd.GetBytes(bytes.Length)); pd.IterationCount /= 2; Assert.AreEqual(bytes, pd.GetBytes(bytes.Length)); } }
public void TestSalt() { using (IPasswordDerivedBytes pd = DerivedBytes(TEST_PASSWORD)) { Assert.AreEqual(DefaultSalt.ToArray(), pd.Salt); byte[] bytes = pd.GetBytes(4); pd.Salt = new Salt().ToArray(); Assert.AreNotEqual(bytes, pd.GetBytes(bytes.Length)); Assert.AreNotEqual(DefaultSalt.ToArray(), pd.Salt); pd.Salt = DefaultSalt.ToArray(); Assert.AreEqual(bytes, pd.GetBytes(bytes.Length)); Assert.AreEqual(DefaultSalt.ToArray(), pd.Salt); } }
public void TestRfcCompatible() { using (IPasswordDerivedBytes pd = DerivedBytes(TEST_PASSWORD)) { pd.IterationCount = 1000; Rfc2898DeriveBytes pbytes = new Rfc2898DeriveBytes(Password.Encoding.GetBytes(TEST_PASSWORD), DefaultSalt.ToArray(), 1000); Assert.AreEqual(pd.GetBytes(20), pbytes.GetBytes(20)); Assert.AreEqual(pd.GetBytes(35), pbytes.GetBytes(35)); byte[] test1 = pd.GetBytes(40); byte[] test2 = pbytes.GetBytes(40); //'RFC' implementation resuses 5 bytes already generated with 'GetBytes(35)', if you always call Reset() these behave the same Assert.AreNotEqual(test1, test2); Buffer.BlockCopy(test2, 5, test2, 0, 35); //strip the first five bytes Array.Resize(ref test1, 35); Array.Resize(ref test2, 35); Assert.AreEqual(test1, test2); //now they should be the same } }
/// <summary> Creates the password from the given bytes and salt </summary> public PasswordKey(IPasswordDerivedBytes derivedBytes, Salt salt) { _derivedBytes = derivedBytes; _salt = salt; _iv = null; }
/// <summary> Creates the password from the given bytes and salt </summary> public PasswordKey(IPasswordDerivedBytes derivedBytes, Salt salt) { _derivedBytes = derivedBytes; _salt = salt; _iv = null; }