Advertisement
Guest User

IEP_Vezbe_2_Glup

a guest
Feb 27th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 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.Data;
  7. using System.Data.SQLite;
  8. using System.Security.Cryptography;
  9.  
  10. namespace IEP_Test{
  11.  
  12.     class Program{
  13.  
  14.         public static string CalculateMD5Hash(string input)
  15.  
  16.         {
  17.  
  18.             // step 1, calculate MD5 hash from input
  19.  
  20.             MD5 md5 = System.Security.Cryptography.MD5.Create();
  21.  
  22.             byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  23.  
  24.             byte[] hash = md5.ComputeHash(inputBytes);
  25.  
  26.             // step 2, convert byte array to hex string
  27.  
  28.             StringBuilder sb = new StringBuilder();
  29.  
  30.             for (int i = 0; i < hash.Length; i++)
  31.  
  32.             {
  33.  
  34.                 sb.Append(hash[i].ToString("X2"));
  35.  
  36.             }
  37.  
  38.             return sb.ToString();
  39.  
  40.         }
  41.  
  42.         static bool login(string username, string password) {
  43.             IDbConnection connection = new SQLiteConnection("Data Source=iep_db_users.db;Version=3;");
  44.  
  45.             //IDbCommand command = new SQLiteCommand("SELECT * FROM Users WHERE Username = @usename AND Password = @password", (SQLiteConnection)connection);
  46.  
  47.             IDbCommand command = new SQLiteCommand("SELECT COUNT(*) FROM Users WHERE Username = @usename AND Password = @password", (SQLiteConnection)connection);
  48.  
  49.  
  50.             IDbDataParameter pUser = command.CreateParameter();
  51.             pUser.Value = username;
  52.             command.Parameters.Add(pUser);
  53.  
  54.             IDbDataParameter pPass = command.CreateParameter();
  55.             pPass.Value = password;
  56.             command.Parameters.Add(pPass);
  57.  
  58.             connection.Open();
  59.             //IDataReader reader = command.ExecuteReader();
  60.             int cnt = (int)command.ExecuteScalar();   //kad se vraca count i tako ta sranja
  61.                                                          
  62.             connection.Close();
  63.  
  64.             /*
  65.             if (reader.Read())
  66.             {
  67.                 Console.WriteLine("User: " + reader[0] + " Username: " + reader[1] + " Password: " + reader[2]);
  68.                 return true;
  69.             }
  70.             return false;
  71.             */
  72.  
  73.             if (cnt != 0)
  74.                 return true;
  75.             return false;
  76.         }
  77.  
  78.         static void Main(string[] args) {
  79.  
  80.             Console.WriteLine("Username: ");
  81.             string username = Console.ReadLine();
  82.             Console.WriteLine("Password: ");
  83.             string password = Console.ReadLine();
  84.  
  85.             password = CalculateMD5Hash(password);
  86.  
  87.             if (login(username, password))
  88.             {
  89.                 Console.WriteLine("HotDog");
  90.             }
  91.             else {
  92.                 Console.WriteLine("Not HotDog");
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement