Guest User

Untitled

a guest
Oct 26th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. public static string myConnectionString = "SERVER=localhost;" + "DATABASE=cd;" + "UID=root;" + "PASSWORD=;";
  2.  
  3.         public static MySqlConnection connection;
  4.         public static MySqlCommand command;
  5.         public static MySqlDataReader Reader;
  6.  
  7.         public static class PasswordDerivation
  8.         {
  9.  
  10.             public const int defaultSaltSize = 16;
  11.             public const int defaultKeySize = 16;
  12.             public const int defaultIterations = 15000;
  13.  
  14.             public static string Derive(string plainPassword, int saltSize = defaultSaltSize, int iterations = defaultIterations, int keySize = defaultKeySize)
  15.             {
  16.            
  17.                 using (var derive = new Rfc2898DeriveBytes(plainPassword, saltSize: saltSize, iterations: iterations))
  18.                 {
  19.                    
  20.                     var b64Pwd = Convert.ToBase64String(derive.GetBytes(keySize));
  21.                     var b64Salt = Convert.ToBase64String(derive.Salt);
  22.                    
  23.                     return string.Join(":", b64Salt, iterations.ToString(), keySize.ToString(), b64Pwd);
  24.                 }
  25.             }
  26.             public static bool Verify(string saltedPassword, string plainPassword)
  27.             {
  28.  
  29.                 var passwordParts = saltedPassword.Split(':');
  30.                
  31.                 var salt = Convert.FromBase64String(passwordParts[0]);
  32.                 var iters = int.Parse(passwordParts[1]);
  33.                 var keySize = int.Parse(passwordParts[2]);
  34.                 var pwd = Convert.FromBase64String(passwordParts[3]);
  35.                
  36.                 using (var derive = new Rfc2898DeriveBytes(plainPassword, salt: salt, iterations: iters))
  37.                 {
  38.                     var hashedInput = derive.GetBytes(keySize);
  39.                     // we ensure that the resulting salted hash is equal to our original hash, if so, the two passwords match.
  40.                     return hashedInput.SequenceEqual(pwd);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         public static Boolean CreateAccount(string username, string password)
  46.         {
  47.  
  48.             String saltedPassword = PasswordDerivation.Derive(password);
  49.  
  50.             bool result = false;
  51.             try
  52.             {
  53.                 using (connection = new MySqlConnection(myConnectionString))
  54.                 using (command = connection.CreateCommand())
  55.  
  56.                     command.CommandText = "INSERT INTO AccountsTable (username, password) VALUES (@username, @password)";
  57.                 command.Prepare();
  58.  
  59.                 command.Parameters.AddWithValue("@username", username);
  60.                 command.Parameters.AddWithValue("@password", saltedPassword);
  61.            
  62.                 result = command.ExecuteNonQuery() > 0;
  63.             }
  64.    
  65.             catch (Exception err) { Console.WriteLine(err); }
  66.             finally { connection.Close(); }
  67.             return result;
  68.         }
  69.  
  70.         public static Boolean LoginAccount(string username, string password)
  71.         {
  72.             bool result = false;
  73.             try
  74.             {
  75.                 using (connection = new MySqlConnection(myConnectionString))
  76.                 using (command = connection.CreateCommand())
  77.                  
  78.                     command.CommandText = "SELECT password FROM AccountsTable WHERE username=@username";
  79.                 command.Prepare();
  80.  
  81.                 command.Parameters.AddWithValue("@username", username);
  82.  
  83.                 connection.Open();
  84.  
  85.                 string saltedPassword = (string)command.ExecuteScalar();
  86.  
  87.                 result = PasswordDerivation.Verify(saltedPassword, password);
  88.             }
  89.             catch (Exception err) { Console.WriteLine(err); }
  90.             finally { connection.Close(); }
  91.             return result;
  92.         }
  93.  
  94. [Command("login","[Usage] /login Username Password")]
  95.         public void LoginCommand(Client player, string username, string password)
  96.         {
  97.  
  98.             var result = CnRGamemode.CreateAccount(username, password);
  99.             if (result)
  100.             {
  101.                 CreateAccount(username, password);
  102.             }
  103.             else
  104.             {
  105.                
  106.             }
  107.  
  108.         }
  109.  
  110.         [Command("register", "[Usage] /register Username Password")]
  111.         public void RegisterCommand(Client player, string username, string password)
  112.         {
  113.             var result = CnRGamemode.LoginAccount(username, password);
  114.             if (result)
  115.             {
  116.                 LoginAccount(username, password);
  117.             }
  118.             else
  119.             {
  120.                 // password is wrong, do anything!
  121.             }
  122.         }
Add Comment
Please, Sign In to add comment