Wednesday, December 14, 2011

Wednesday 12.14.11

Imports System
Imports System.Security.Cryptography

Module Module1

Sub Main()
Apress.VisualBasicRecipes.Chapter12.Recipe12_12.Main()
End Sub

End Module

Namespace Apress.VisualBasicRecipes.Chapter12
Public Class Recipe12_12
Public Shared Sub Main()
'create a byte array to hold the random data
Dim number As Byte() = New Byte(32) {}
'instantiate the default random number generator
Dim rng As RandomNumberGenerator = RandomNumberGenerator.Create

'generate 32 bytes of random data
rng.GetBytes(number)

'display the random number
Console.WriteLine(BitConverter.ToString(number))

'wait to continue
Console.WriteLine(Environment.NewLine)
Console.WriteLine("Main method complete. Press Enter.")
Console.ReadLine()
End Sub
End Class
End Namespace


Imports System
Imports System.Text

Imports System.Security.Cryptography

Module Module1

Sub Main()
Dim args As String() = {"SHA384CryptoServiceProvider", "nasal7Spray"}
Recipe12_13.Main(args)

End Sub

End Module

Public Class Recipe12_13

Public Shared Sub Main(ByVal args As String())
'create a hashalgorithm of the type specified by the first command-line argument
Dim hashAlg As HashAlgorithm = Nothing

'some of the lcasses cannot be instantiated using the factory method so they must be directly created
Select Case args(0).ToUpper()
Case "SHA1MANAGED"
hashAlg = New SHA1Managed
Case "SHA256CRYPTOSERVICEPROVIDER"
hashAlg = New SHA256CryptoServiceProvider
Case "SHA384CRYPTOSERVICEPROVIDER"
hashAlg = New SHA384CryptoServiceProvider
Case "SHA512CRYPTOSERVICEPROVIDER"
hashAlg = New SHA512CryptoServiceProvider
Case Else
hashAlg = HashAlgorithm.Create(args(0))
End Select

Using hashAlg
'convert the password string, provided as the second command-line arguemnt
'to an array of bytes
Dim pwordData As Byte() = Encoding.Default.GetBytes(args(1)) 'imports system.text
'generate the has code of the password
Console.WriteLine("Generate the hash code of the password")
Dim hash As Byte() = hashAlg.ComputeHash(pwordData)

'display the hash code of the password to the console
Console.WriteLine(BitConverter.ToString(hash))

'wait to continue
Console.WriteLine(Environment.NewLine)
Console.WriteLine("Main method complete. Press Enter.")
Console.ReadLine()
End Using
End Sub
End Class


Imports System
Imports System.IO
Imports System.Security.Cryptography

Module Module1

Sub Main()
Dim args As String() = {"SHA1Managed", "C:/new/VBDotNet.txt"}
Recipe12_14.Main(args)

End Sub

End Module

Public Class Recipe12_14

Public Shared Sub Main(ByVal args As String())
'create a hash algorith of the tpye specified by the first command-line argument
Dim hashAlg As HashAlgorithm = Nothing
'the sha1 managed algorithm cannot be implemented using the
'factory approach it must be instantiated directly
If args(0).CompareTo("SHA1Managed") = 0 Then
hashAlg = New SHA1Managed
Else
hashAlg = HashAlgorithm.Create(args(0))
End If

'open a FileStream to teh file specified by the second command-line argument
Using fileArg As New FileStream(args(1), FileMode.Open, FileAccess.Read)
'generate the hash code of the password
Dim hash As Byte() = hashAlg.ComputeHash(fileArg)
'display the hash code fo the password to the console
Console.WriteLine(BitConverter.ToString(hash))
'wait to continue
Console.ReadLine()
End Using
End Sub
End Class


Imports System
Imports System.IO
Imports System.Security.Cryptography

Module Module1

Sub Main()
Dim args As String() = {"SHA1Managed", "C:/new/VBDotNet.txt"}
Recipe12_14.Main(args)

End Sub

End Module

Public Class Recipe12_14

Public Shared Sub Main(ByVal args As String())
'create a hash algorith of the tpye specified by the first command-line argument
Dim hashAlg As HashAlgorithm = Nothing
'the sha1 managed algorithm cannot be implemented using the
'factory approach it must be instantiated directly
If args(0).CompareTo("SHA1Managed") = 0 Then
hashAlg = New SHA1Managed
Else
hashAlg = HashAlgorithm.Create(args(0))
End If

readHash(args(1), hashAlg)

Dim myFileStream As System.IO.FileStream

Try
myFileStream = New System.IO.FileStream(args(1), FileMode.Append, FileAccess.Write, FileShare.None)
Try
Dim myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine("Hello World!")
'flush before we close
myWriter.Flush()
myWriter.Close()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
Catch ex As Exception
MsgBox(ex.ToString())
Finally
myFileStream.Close()
End Try

readHash(args(1), hashAlg)
End Sub

Public Shared Sub readHash(ByVal fileName, ByVal hashAlg)
'open a FileStream to teh file specified by the second command-line argument
Using fileArg As New FileStream(fileName, FileMode.Open, FileAccess.Read)
'generate the hash code of the password
Dim hash As Byte() = hashAlg.ComputeHash(fileArg)
'display the hash code fo the password to the console
Console.WriteLine(BitConverter.ToString(hash))
'wait to continue
Console.WriteLine()
Console.ReadLine()
End Using
End Sub
End Class



namespace practice
{
class Program
{
static void Main(string[] args)
{
EntryPoint ep = new EntryPoint();
ep.Run();
Console.ReadLine();
}
}

public struct Coordinate //this is a value type
{
public int x;
public int y;
}

public class EntryPoint //this is a reference type
{
public static void AttemptToModifyCoord(Coordinate coord)
{
coord.x = 1;
coord.y = 1;
}

public static void ModifyCoord(ref Coordinate coord)
{
coord.x = 10;
coord.y = 10;
}

public void Run()
{
Coordinate location;
location.x = 50;
location.y = 50;

AttemptToModifyCoord(location);
System.Console.WriteLine("({0}, {1} )", location.x, location.y);

ModifyCoord(ref location);
System.Console.WriteLine("({0}, {1} )", location.x, location.y);
}
}
}


class Program
{
[Flags]
public enum AccessFlags
{
NoAccess = 0x0,
ReadAccess = 0x1,
WriteAccess = 0x2,
ExecuteAccess = 0x4
}

static void Main(string[] args)
{
AccessFlags access = AccessFlags.ReadAccess | AccessFlags.WriteAccess;
System.Console.WriteLine("Access is {0}", access);
}
}


static void Main(string[] args)
{
var myList = new List();

myList.Add(1);
myList.Add(2);
myList.Add(3);

foreach (var i in myList)
{
Console.WriteLine(i);
}
}


Imports System
Imports System.Reflection
Imports System.Security.Policy


Module ADSetupInformation

Sub Main()
'create the application domain setup information
Dim domainInfo As New AppDomainSetup()
domainInfo.ApplicationBase = System.Environment.CurrentDirectory
'create evidence for a new AppDomain
Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence

'create the new application domain using setup information
Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain", adevidence, domainInfo)

'write out the domain information
Console.WriteLine("Host Domain: " + AppDomain.CurrentDomain.FriendlyName)
Console.WriteLine("New child domain: " + domain.FriendlyName)
Console.WriteLine()
Console.WriteLine("Application base is: " + domain.SetupInformation.ApplicationBase)

AppDomain.Unload(domain)
Console.ReadLine()
End Sub

End Module

No comments: