Загрузка данных


Если класс HashingService не работает, давайте убедимся, что он правильно реализован. Вот полный код с исправлениями и проверкой на работоспособность:

using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        HashingService hashingService = new HashingService();
        bool continueRunning = true;

        while (continueRunning)
        {
            Console.WriteLine("Выберите алгоритм хеширования:");
            Console.WriteLine("1. MD5");
            Console.WriteLine("2. SHA-1");
            Console.WriteLine("3. SHA-256");
            Console.WriteLine("0. Выход");
            Console.Write("Ваш выбор: ");

            string choice = Console.ReadLine();

            if (choice == "0")
            {
                continueRunning = false;
                continue;
            }

            Console.Write("Введите строку для хеширования: ");
            string input = Console.ReadLine();

            try
            {
                string hash = hashingService.ComputeHash(input, choice);
                Console.WriteLine($"Хеш: {hash}");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"Ошибка: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Произошла ошибка: {ex.Message}");
            }

            Console.WriteLine();
        }
    }
}

public class HashingService
{
    public string ComputeHash(string input, string algorithm)
    {
        switch (algorithm)
        {
            case "1":
                return ComputeMD5Hash(input);
            case "2":
                return ComputeSHA1Hash(input);
            case "3":
                return ComputeSHA256Hash(input);
            default:
                throw new ArgumentException("Неверный выбор алгоритма.");
        }
    }

    private string ComputeMD5Hash(string input)
    {
        using (MD5 md5 = MD5.Create())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
        }
    }

    private string ComputeSHA1Hash(string input)
    {
        using (SHA1 sha1 = SHA1.Create())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = sha1.ComputeHash(inputBytes);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
        }
    }

    private string ComputeSHA256Hash(string input)
    {
        using (SHA256 sha256 = SHA256.Create())
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = sha256.ComputeHash(inputBytes);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
        }
    }
}


▎Убедитесь в следующем:

1. Использование пространства имен: Убедитесь, что вы не используете дополнительные пространства имен, которые могут конфликтовать с кодом.
2. Проверка сборок: Убедитесь, что у вас есть ссылки на необходимые сборки (System.Security.Cryptography, System.Text), которые обычно включены по умолчанию в проектах .NET.
3. Запуск программы: Убедитесь, что вы запускаете программу в среде, которая поддерживает консольный ввод/вывод.

▎Как протестировать:

1. Скопируйте весь код в новый проект C#.
2. Запустите проект.
3. Следуйте инструкциям в консоли для выбора алгоритма и ввода строки для хеширования.

Если возникают ошибки, пожалуйста, предоставьте информацию о том, какие именно ошибки появляются, чтобы я мог помочь вам более конкретно.