Library/Library.Test/TestHashDerivedBytes.cs
C# | Copy Code |
---|---|
using (IPasswordDerivedBytes pd = DerivedBytes(TEST_PASSWORD)) { pd.IterationCount = 1000; byte[] sha1Hash = SHA1.Create().ComputeHash(Password.Encoding.GetBytes(TEST_PASSWORD)); Rfc2898DeriveBytes pbytes = new Rfc2898DeriveBytes(sha1Hash, 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 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 } |
VB.NET | Copy Code |
---|---|
Using pd As IPasswordDerivedBytes = DerivedBytes(TEST_PASSWORD) pd.IterationCount = 1000 Dim sha1Hash As Byte() = SHA1.Create().ComputeHash(Password.Encoding.GetBytes(TEST_PASSWORD)) Dim pbytes As New Rfc2898DeriveBytes(sha1Hash, DefaultSalt.ToArray(), 1000) Assert.AreEqual(pd.GetBytes(20), pbytes.GetBytes(20)) Assert.AreEqual(pd.GetBytes(35), pbytes.GetBytes(35)) Dim test1 As Byte() = pd.GetBytes(40) Dim test2 As Byte() = pbytes.GetBytes(40) ''RFC' implementation resuses 5 bytes already generated with 'GetBytes(35)', if you call reset these behave the same Assert.AreNotEqual(test1, test2) Buffer.BlockCopy(test2, 5, test2, 0, 35) 'strip the first five bytes Array.Resize(test1, 35) Array.Resize(test2, 35) 'now they should be the same Assert.AreEqual(test1, test2) End Using |
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7