Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. using MySql.Data.MySqlClient;
  8. namespace login
  9. {
  10.  
  11.     class Program
  12.     {
  13.         static string connString = "SERVER=shadowdriz.lima-db.de;" +
  14.                                 "DATABASE=db_353486_1;" +
  15.                                 "UID=USER353486;" +
  16.                                 "PASSWORD=censored girl dont even look. XD;";
  17.         static void Main(string[] args)
  18.         {
  19.         A:
  20.             Console.Clear();
  21.             string id, pw, answer;
  22.             Console.Write("id:");
  23.             id = Console.ReadLine();
  24.             Console.Write("pw:");
  25.             pw = ReadPassword();
  26.             if (IsValid(id, pw))
  27.             {
  28.                 Console.WriteLine("Successfully Logged in!");
  29.             }
  30.             else
  31.             {
  32.                 Console.WriteLine("Wrong Credentials!");
  33.                 Console.WriteLine("Do you want to sign up? y/n");
  34.                 if ((answer = Console.ReadLine()) == "y")
  35.                     SignUp();
  36.                 else
  37.                     goto A;
  38.             }
  39.         }
  40.         static void SignUp()
  41.         {
  42.             Console.Clear();
  43.             Console.Write("id:");
  44.             string id = Console.ReadLine();
  45.             B:
  46.             Console.Write("pw:");
  47.             string pw = ReadPassword();
  48.             Console.Write("pw:");
  49.             string pw2 = ReadPassword();
  50.             if (pw == pw2)
  51.             {
  52.                 Console.WriteLine("Signing up..");
  53.                 Console.WriteLine("Checking if id is available..");
  54.  
  55.             }
  56.             else
  57.             {
  58.                 Console.WriteLine("Your passwords do not match! Try again!");
  59.                 goto B;
  60.             }
  61.         }
  62.         static bool IsValid(string id, string pw)
  63.         {
  64.             using (MySqlConnection conn = new MySqlConnection(connString))
  65.             {
  66.                 MySqlCommand cmd = conn.CreateCommand();
  67.                 cmd.CommandText = "select password from users where username='@username';";
  68.                 conn.Open();
  69.                 MySqlDataReader reader = cmd.ExecuteReader();
  70.                 while (reader.Read())
  71.                 {
  72.                     if (reader.HasRows)
  73.                     {
  74.                         string password = reader.GetString(0);
  75.                         if ((pw = GetMD5(pw)) == password)
  76.                         {
  77.                             return true;
  78.                         }
  79.                     }
  80.                 }
  81.                 return false;
  82.             }
  83.         }
  84.         static string GetMD5(string text2hash)
  85.         {
  86.             StringBuilder sb = new StringBuilder();
  87.             using (MD5 md5 = MD5.Create())
  88.             {
  89.                 byte[] data = md5.ComputeHash(ASCIIEncoding.UTF8.GetBytes(text2hash));
  90.                 for (int i = 0; i < data.Length; i++)
  91.                 {
  92.                     sb.Append(data[i].ToString("x2"));
  93.                 }
  94.             }
  95.             return sb.ToString();
  96.         }
  97.         static string ReadPassword()
  98.         {
  99.             StringBuilder sb = new StringBuilder();
  100.             while (true)
  101.             {
  102.                 ConsoleKeyInfo i = Console.ReadKey(true);
  103.                 if (i.Key == ConsoleKey.Enter)
  104.                 {
  105.                     Console.WriteLine();
  106.                     break;
  107.                 }
  108.                 if (i.Key == ConsoleKey.Backspace)
  109.                 {
  110.                     if (sb.Length > 0)
  111.                     {
  112.                         Console.Write("\b \b");
  113.                         sb.Length--;
  114.                         continue;
  115.                     }
  116.                 }
  117.                 Console.Write('*');
  118.                 sb.Append(i.KeyChar);
  119.             }
  120.             return sb.ToString();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement