[Category ("NotWorking")] // bug #79499 public void LongMultipleGetBytes () { // based on http://bugzilla.ximian.com/show_bug.cgi?id=79499 PasswordDeriveBytes pd = new PasswordDeriveBytes ("mono", new byte[20]); string key = BitConverter.ToString (pd.GetBytes (32)); Assert.AreEqual ("88-0A-AE-0A-41-61-02-78-FD-E2-70-9F-25-13-14-28-1F-C7-D9-72-9A-AE-CA-3F-BD-31-B4-F0-BD-8E-5B-98", key, "key"); string iv = BitConverter.ToString (pd.GetBytes (16)); Assert.AreEqual ("FD-E2-70-9F-25-13-14-28-4D-3F-9B-F8-EE-AA-95-ED", iv, "iv"); pd.Reset (); // bytes from 32-40 are different from calling GetBytes separately Assert.AreEqual (key + "-F6-55-6C-3E-54-8B-F3-73-4D-3F-9B-F8-EE-AA-95-ED", BitConverter.ToString (pd.GetBytes (48)), "same"); }
// generate the key up to HashSize and reset between operations public void ShortRun(string msg, PasswordDeriveBytes pd, byte[] finalKey) { for (int i=0; i < finalKey.Length; i++) { int j = 0; bool compare = true; byte[] key = pd.GetBytes (i+1); for (; j < i; j++) { if (finalKey [j] != key[j]) { compare = false; break; } } Assert.IsTrue (compare, msg + " #" + j); pd.Reset (); } }
public void Properties () { // create object... PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000); Assert.AreEqual ("MD5", pd.HashName, "HashName-MD5"); Assert.AreEqual (1000, pd.IterationCount, "IterationCount-1000"); // ...then change all its properties... pd.HashName = "SHA1"; Assert.AreEqual ("SHA1", pd.HashName, "HashName-SHA1"); pd.Salt = salt; Assert.AreEqual (ssalt, BitConverter.ToString (pd.Salt), "Salt"); pd.IterationCount = 1; Assert.AreEqual (1, pd.IterationCount, "IterationCount-1"); byte[] expectedKey = { 0x0b, 0x61, 0x93, 0x96 }; // ... before using it Assert.AreEqual (expectedKey, pd.GetBytes (4), "PKCS#5 test properties"); // it should work but if we try to set any properties after GetBytes // they should all throw an exception try { pd.HashName = "SHA256"; Assert.Fail ("PKCS#5 can't set HashName after GetBytes - expected CryptographicException but got none"); } catch (CryptographicException) { // do nothing, this is what we expect } catch (Exception e) { Assert.Fail ("PKCS#5 can't set HashName after GetBytes - expected CryptographicException but got " + e.ToString ()); } try { pd.Salt = expectedKey; Assert.Fail ("PKCS#5 can't set Salt after GetBytes - expected CryptographicException but got none"); } catch (CryptographicException) { // do nothing, this is what we expect } catch (Exception e) { Assert.Fail ("PKCS#5 can't set Salt after GetBytes - expected CryptographicException but got " + e.ToString ()); } try { pd.IterationCount = 10; Assert.Fail ("PKCS#5 can't set IterationCount after GetBytes - expected CryptographicException but got none"); } catch (CryptographicException) { // do nothing, this is what we expect } catch (Exception e) { Assert.Fail ("PKCS#5 can't set IterationCount after GetBytes - expected CryptographicException but got " + e.ToString ()); } pd.Reset (); // finally a useful reset :) pd.HashName = "SHA256"; pd.Salt = expectedKey; pd.IterationCount = 10; }
// Reset the state. public override void Reset() { db.Reset(); }