Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- namespace PasswordHashing
- {
- class Program
- {
- static readonly List<string> storedHashes = new List<string>();
- static void Main(string[] args)
- {
- Console.Write("Введите пароль: ");
- string password = Console.ReadLine();
- string hash = HashPassword(password);
- storedHashes.Add(hash);
- Console.WriteLine($"Пароль: {password}");
- Console.WriteLine($"Хэш: {hash}");
- Console.Write("\nВведите пароль для проверки:");
- string input = Console.ReadLine();
- if (VerifyPassword(input))
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Пароль верный!");
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Неверный пароль!");
- }
- Console.ResetColor();
- }
- static string HashPassword(string password)
- {
- using (SHA256 sha256 = SHA256.Create())
- {
- byte[] bytes = Encoding.UTF8.GetBytes(password);
- byte[] hashBytes = sha256.ComputeHash(bytes);
- StringBuilder builder = new StringBuilder();
- foreach (byte b in hashBytes)
- {
- builder.Append(b.ToString("x2"));
- }
- return builder.ToString();
- }
- }
- static bool VerifyPassword(string inputPassword)
- {
- string inputHash = HashPassword(inputPassword);
- return storedHashes.Contains(inputHash);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement