usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Security.Cryptography;namespacePasswordCrackerServer{classPasswordFileHandler{privatestaticreadonlyConverter<char,byte>Converter=CharToByte;/// <summary>/// With this method you can make you own password file/// </summary>/// <param name="filename">Name of password file</param>/// <param name="usernames">List of usernames</param>/// <param name="passwords">List of passwords in clear text</param>/// <exception cref="ArgumentException">if usernames and passwords have different lengths</exception>publicstaticvoidWritePasswordFile(Stringfilename,String[]usernames,String[]passwords){HashAlgorithmmessageDigest=newSHA1CryptoServiceProvider();if(usernames.Length!=passwords.Length){thrownewArgumentException("usernames and passwords must be same lengths");}using(FileStreamfs=newFileStream(filename,FileMode.CreateNew,FileAccess.Write))using(StreamWritersw=newStreamWriter(fs)){for(inti=0;i<usernames.Length;i++){byte[]passwordAsBytes=Array.ConvertAll(passwords[i].ToCharArray(),GetConverter());byte[]encryptedPassword=messageDigest.ComputeHash(passwordAsBytes);Stringline=usernames[i]+":"+Convert.ToBase64String(encryptedPassword)+"\n";sw.WriteLine(line);}}}/// <summary>/// Reads all the username + encrypted password from the password file/// </summary>/// <param name="filename">the name of the password file</param>/// <returns>A list of (username, encrypted password) pairs</returns>publicstaticList<UserInfo>ReadPasswordFile(Stringfilename){List<UserInfo>result=newList<UserInfo>();FileStreamfs=newFileStream(filename,FileMode.Open,FileAccess.Read);using(StreamReadersr=newStreamReader(fs)){while(!sr.EndOfStream){Stringline=sr.ReadLine();String[]parts=line.Split(":".ToCharArray());UserInfouserInfo=newUserInfo(parts[0],parts[1]);result.Add(userInfo);}returnresult;}}publicstaticConverter<char,byte>GetConverter(){returnConverter;}/// <summary>/// Converting a char to a byte can be done in many ways./// This is one way .../// </summary>/// <param name="ch"></param>/// <returns></returns>privatestaticbyteCharToByte(charch){returnConvert.ToByte(ch);}}}