1using System; 2using System.Security.Cryptography; 3using System.Net; 4 5namespace ALittleClass ...{ 6 class Program ...{ 7 static void Main ( string[ ] args ) ...{ 8 // create the md5 provider 9 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ( ); 10 // you can hash any array of bytes or a stream... 11 // for this example, i will get google.com and hash the response stream 12 WebRequest req = WebRequest.Create( "http://google.com/" ); 13 Console.WriteLine ( "Retrieving {0}", req.RequestUri.ToString ( ) ); 14 // get the response 15 WebResponse resp = req.GetResponse( ); 16 Console.WriteLine ( "Computing the hash..." ); 17 // hash the stream 18 byte[] hash = md5.ComputeHash ( resp.GetResponseStream ( ) ); 19 // base64 encode the hash 20 string b64 = Convert.ToBase64String ( hash ); 21 Console.WriteLine ( "Google Hash: {0}", b64 ); 22 Console.ReadLine ( ); 23 } 24 } 25}
Retrieving http://google.com/Computing the hash...Google Hash: /PX2eXxl/jvS/7bNW1sP+g==
1using System; 2using System.Security; 3using System.Runtime.InteropServices; 4 5namespace ALittleClass ...{ 6 class Program ...{ 7 static void Main ( string[] args ) ...{ 8 // create the secure string 9 SecureString password = new SecureString ( ); 10 // prompt for the password 11 Console.Write ( "Enter Password:" ); 12 // read the keys pressed by the user 13 ConsoleKeyInfo nextKey = Console.ReadKey(true); 14 // read the password entered from standard input. 15 while ( nextKey.Key != ConsoleKey.Enter ) ...{ 16 // check if the user is backspacing to make a correction... 17 if ( nextKey.Key == ConsoleKey.Backspace && password.Length > 0 ) ...{ 18 // remove the character from the secure string 19 password.RemoveAt ( password.Length - 1 ); 20 // erase the character on the console. 21 Console.Write ( nextKey.KeyChar ); 22 Console.Write ( " " ); 23 Console.Write ( nextKey.KeyChar ); 24 } else ...{ 25 // add the character to the secure string 26 password.AppendChar ( nextKey.KeyChar ); 27 // display a character in the console 28 Console.Write ( "*" ); 29 } 30 nextKey = Console.ReadKey ( true ); 31 } 32 Console.WriteLine ( ); 33 34 // lock the password down so it can not be changed further 35 password.MakeReadOnly ( ); 36 37 // so we can read the value we need to do some conversion... 38 IntPtr bstr = Marshal.SecureStringToBSTR ( password ); 39 string encPassword = Marshal.PtrToStringAuto(bstr); 40 // destroy the password and remove from memory. 41 password.Dispose ( ); 42 43 Console.WriteLine ( "Secure password: {0}", encPassword ); 44 Console.WriteLine ( "Press Enter To Continue..." ); 45 Console.ReadLine ( ); 46 } 47 } 48}
Enter Password:******************** Secure password: i entered a password Press Enter To Continue...
1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.IO; 5 6namespace ALittleClass ...{ 7 class Program ...{ 8 static void Main ( string[ ] args ) ...{ 9 // get a file 10 FileInfo file = new FileInfo ( typeof ( Program ).Assembly.Location ); 11 // output the full file name 12 Console.WriteLine ( file.FullName ); 13 // changes the extension of the file and returns the new filename 14 Console.WriteLine ( Path.ChangeExtension ( file.FullName, "txt" ) ); 15 // combines 2 paths together 16 Console.WriteLine ( Path.Combine ( file.Directory.FullName, "myFile.txt" ) ); 17 // gets only the directory name 18 Console.WriteLine ( Path.GetDirectoryName ( file.FullName ) ); 19 // gets only the file extension (includes the dot) 20 Console.WriteLine ( Path.GetExtension ( file.FullName ) ); 21 // gets only the file name (with the extension) 22 Console.WriteLine ( Path.GetFileName ( file.FullName ) ); 23 // gets only the file name with out the extension and the dot 24 Console.WriteLine ( Path.GetFileNameWithoutExtension ( file.FullName ) ); 25 // gets the full path of a file name 26 Console.WriteLine ( Path.GetFullPath ( file.Name ) ); 27 // gets an array of characters that are invalid in a file name 28 Console.WriteLine ( new string ( Path.GetInvalidFileNameChars ( ) ) ); 29 // gets an array of characters that are invalid in a path 30 Console.WriteLine ( new string ( Path.GetInvalidPathChars ( ) ) ); 31 // Gets the root path of a path 32 Console.WriteLine ( Path.GetPathRoot ( file.FullName ) ); 33 // gets a random file name 34 Console.WriteLine ( Path.GetRandomFileName ( ) ); 35 // gets a temp file name 36 Console.WriteLine ( Path.GetTempFileName ( ) ); 37 // gets the temp path 38 Console.WriteLine ( Path.GetTempPath ( ) ); 39 // checks if a file has an extension 40 Console.WriteLine ( Path.HasExtension ( file.FullName ) ); 41 // checks if a path is rooted 42 Console.WriteLine ( Path.IsPathRooted ( file.FullName ) ); 43 // gets the character that separates directories 44 Console.WriteLine ( Path.DirectorySeparatorChar ); 45 // gets the alternate character that can be used to separate directories 46 Console.WriteLine ( Path.AltDirectorySeparatorChar ); 47 // gets the character used to separate multiple paths 48 Console.WriteLine ( Path.PathSeparator ); 49 // gets the character used to separate the volume from the path 50 Console.WriteLine ( Path.VolumeSeparatorChar ); 51 Console.ReadLine ( ); 52 } 53 } 54} 55 56
H:\dotNetProjects\ALittleClass\ALittleClass\bin\Debug\ALittleClass.exe H:\dotNetProjects\ALittleClass\ALittleClass\bin\Debug\ALittleClass.txt H:\dotNetProjects\ALittleClass\ALittleClass\bin\Debug\myFile.txt H:\dotNetProjects\ALittleClass\ALittleClass\bin\Debug .exe ALittleClass.exe ALittleClass H:\dotNetProjects\ALittleClass\ALittleClass\bin\Debug\ALittleClass.exe "<>| ☺☻♥♦ ♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼:*?\/ "<>| ☺☻♥♦ ♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ H:\ r2cm2y2c.fku C:\Users\Ryan\AppData\Local\Temp\tmpE5E5.tmp C:\Users\Ryan\AppData\Local\Temp\ True True \ / ; :