Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Npgsql;
- namespace Учебная_практика_БД
- {
- public partial class RegistrationForm : Form
- {
- private readonly Regex loginRegex = new Regex("[А-Яа-я0-9]{6,50}$");
- private readonly string sConnStr = new NpgsqlConnectionStringBuilder
- {
- Host = "localhost",
- Port = 5432,
- Database = "user_registration",
- Username = "pg_student",
- Password = "pg_student"
- }.ConnectionString;
- private static byte[] CalcHash(string filePath)
- {
- return Org.BouncyCastle.Security.DigestUtilities.CalculateDigest("GOST3411-2012-256",
- System.IO.File.ReadAllBytes(filePath)
- );
- }
- public RegistrationForm()
- {
- InitializeComponent();
- }
- private void BtLoad_Click(object sender, EventArgs e)
- {
- if(ofdImage.ShowDialog() == DialogResult.OK)
- {
- password.ImageLocation = ofdImage.FileName;
- }
- }
- private void TbLogin_TextChanged(object sender, EventArgs e)
- {
- if (!loginRegex.IsMatch(tbLogin.Text))
- {
- epMain.SetError(tbLogin, "Логин должен быть длиной от 6 до 50 символов и содержать только русские буквы и цифры!");
- btReg.Enabled = false;
- return;
- }
- using (var sConn = new NpgsqlConnection(sConnStr))
- {
- sConn.Open();
- var sCommand = new NpgsqlCommand
- {
- Connection = sConn,
- CommandText = @"
- SELECT COUNT(*)
- FROM users
- WHERE lower(login) = lower(@currentLogin)"
- };
- sCommand.Parameters.AddWithValue("@currentLogin", tbLogin.Text);
- if((long) sCommand.ExecuteScalar() > 0)
- {
- epMain.SetError(tbLogin, "Логин уже занят");
- return;
- }
- }
- btReg.Enabled = true;
- epMain.SetError(tbLogin, "");
- }
- private void BtReg_Click(object sender, EventArgs e)
- {
- using (var sConn = new NpgsqlConnection(sConnStr))
- {
- sConn.Open();
- var sCommand = new NpgsqlCommand
- {
- Connection = sConn,
- CommandText = @"INSERT INTO users(login, image_hash) VALUES(@currentLogin, @imageHash)"
- };
- sCommand.Parameters.AddWithValue("@currentLogin", tbLogin.Text);
- sCommand.Parameters.AddWithValue("@imageHash",
- CalcHash(password.ImageLocation));
- sCommand.ExecuteNonQuery();
- }
- }
- }
- }
- /*
- * Реализовать приложение, которое позволяет регистрировать пользователя. Сделать пароль текстовым.
- * изменить хеш функцию так, чтобы одинковые пароли не давали одинаковые хеш функции, используя соль
- * реализовать аутентификацию. Проверять пароль на сложность. В логине можно ставить пробелы.
- *
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement