CSharpTest.Net
GetBytes Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace > IPasswordDerivedBytes Interface : GetBytes Method

cb

Glossary Item Box

Returns a pseudo-random key from a password, salt and iteration count.

Syntax

Visual Basic (Declaration) 
Function GetBytes( _
   ByVal cb As Integer _
) As Byte()
C# 
byte[] GetBytes( 
   int cb
)

Parameters

cb

Example

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.NETCopy 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

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys