CSharpTest.Net
FromBytes Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace > Salt Class : FromBytes Method

bytes
An array of random bytes of 8, 16, 32, or 64 bytes long

Glossary Item Box

Creates salt from the provided bytes or a hash of the bytes

Syntax

Visual Basic (Declaration) 
Public Shared Function FromBytes( _
   ByVal bytes() As Byte _
) As Salt
C# 
public static Salt FromBytes( 
   byte[] bytes
)

Parameters

bytes
An array of random bytes of 8, 16, 32, or 64 bytes long

Example

Library/Library.Test/TestSalt.cs

C#Copy Code
byte[] testValid = Encoding.ASCII.GetBytes("12345678");
Assert.AreEqual(Salt.Size.b64, Salt.FromBytes(testValid).BitSize);
Assert.AreEqual(testValid, Salt.FromBytes(testValid).ToArray());
testValid = null;

//now test with an odd number of bytes, should always hash to Sha256
byte[] notValid = Encoding.ASCII.GetBytes("0123456789");
Assert.AreNotEqual(Salt.Size.b64, Salt.FromBytes(notValid).BitSize);
Assert.AreNotEqual(testValid, Salt.FromBytes(notValid).ToArray());

Assert.AreEqual(Salt.Size.b256, Salt.FromBytes(notValid).BitSize);
Assert.AreEqual(Hash.SHA256(notValid).ToArray(), Salt.FromBytes(notValid).ToArray());
VB.NETCopy Code
Dim testValid As Byte() = Encoding.ASCII.GetBytes("12345678")
Assert.AreEqual(Salt.Size.b64, Salt.FromBytes(testValid).BitSize)
Assert.AreEqual(testValid, Salt.FromBytes(testValid).ToArray())
testValid = Nothing

'now test with an odd number of bytes, should always hash to Sha256
Dim notValid As Byte() = Encoding.ASCII.GetBytes("0123456789")
Assert.AreNotEqual(Salt.Size.b64, Salt.FromBytes(notValid).BitSize)
Assert.AreNotEqual(testValid, Salt.FromBytes(notValid).ToArray())

Assert.AreEqual(Salt.Size.b256, Salt.FromBytes(notValid).BitSize)
Assert.AreEqual(Hash.SHA256(notValid).ToArray(), Salt.FromBytes(notValid).ToArray())

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys