using System; using System.IO; using System.Text; using System.Security.Cryptography; public class multihash{ public static void Main(String[] args) { byte[] tohash = null; if(args.Length<1){ Console.WriteLine("\nUsage: multihash ") ; return; } String arg1 = args[0] ; //--------- If we have a file, get the binary data ------- if(File.Exists(arg1)){ Console.WriteLine(); Console.WriteLine("Hashing file '{0}' ", arg1); tohash = GetFileBytes(arg1); } else{ ASCIIEncoding ascii = new ASCIIEncoding(); tohash = ascii.GetBytes(arg1); Console.WriteLine(); Console.WriteLine("Hashing string '{0}' ", arg1); } MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5hash = md5.ComputeHash(tohash); Console.WriteLine(); Console.WriteLine("------------ MD5 hash: ------------"); DisplayBytes(md5hash); Console.WriteLine(); Console.WriteLine(Convert.ToBase64String(md5hash)); SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] sha1hash = sha1.ComputeHash(tohash); Console.WriteLine(); Console.WriteLine("------------ SHA1 hash: ------------"); DisplayBytes(sha1hash); Console.WriteLine(); Console.WriteLine(Convert.ToBase64String(sha1hash)); SHA256 sha256 = new SHA256Managed(); byte[] sha256hash = sha256.ComputeHash(tohash); Console.WriteLine(); Console.WriteLine("------------ SHA256 hash: ------------"); DisplayBytes(sha256hash); Console.WriteLine(); Console.WriteLine(Convert.ToBase64String(sha256hash)); SHA512 sha512 = new SHA512Managed(); byte[] sha512hash = sha512.ComputeHash(tohash); Console.WriteLine(); Console.WriteLine("------------ SHA512 hash: ------------"); DisplayBytes(sha512hash); Console.WriteLine(); Console.WriteLine(Convert.ToBase64String(sha512hash)); } private static byte[] GetFileBytes(String filename){ if(!File.Exists(filename)) return null; Stream stream=new FileStream(filename,FileMode.Open); int datalen = (int)stream.Length; byte[] filebytes =new byte[datalen]; stream.Seek(0,SeekOrigin.Begin); stream.Read(filebytes,0,datalen); stream.Close(); return filebytes; } private static void DisplayBytes(byte[] data) { for(int i=1; i<=data.Length; i++){ Console.Write("{0:X2} ", data[i-1]) ; if(i%16 == 0 && i != data.Length) Console.WriteLine(""); } Console.WriteLine(); } }