using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; namespace mkpasswd { class Program { /// /// Funkcia vygeneruje nahodnu sol pouzivanu pri ukladani odtalcku hesiel pouzivatelov /// /// pocet nahodne vygenerovanych bajtov /// public static string createSalt( int lengthInBytes ) { // pripravim buffer, do ktoreho vygenerujem nahodne cislo byte[] buffer = new byte[ lengthInBytes ]; // pomocny objekt pre generovanie nahody Random rnd = new Random(); // vygenerujem nahodne cislo rnd.NextBytes( buffer ); // vratim nahodu ako Base64 string return Convert.ToBase64String( buffer ); } /// /// Funkcia na vypocet odtlacku zadaneho hesla s pouzitim zadanej soli /// /// heslo /// sol /// public static string crypt( string passwd, string salt ) { // odtlacok budem pocitat z retazca pozostavajuceho z hesla a soli string s = passwd + salt; // retazec skonvertujem na pole bajtov byte[] b = Encoding.UTF8.GetBytes( s ); // pomocny objekt pre vypocet MD5 MD5 md5 = MD5.Create(); // vypocitam odtlacok hesla a soli byte[] h = md5.ComputeHash( b ); // vratim hash ako Base64 string return Convert.ToBase64String( h ); } static void Main( string[] args ) { Console.WriteLine( crypt( "heslo", "02SK64Xv" ) ); } } }