﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;

namespace HashTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = string.Empty;
        }

        private void menuItem2_Click(object sender, RoutedEventArgs e)
        {
            textBox3.Text = string.Empty;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int result = 0;

            result += Convert.ToBoolean(radioButton1.IsChecked) ? 1 : 2;

            result += Convert.ToBoolean(radioButton3.IsChecked) ? 4 : 8;

            byte[] buffer = GetSeed(textBox1.Text);

            MD5 md5 = MD5CryptoServiceProvider.Create(@"MD5");

            byte[] hash = md5.ComputeHash(buffer);

            switch (result)
            {
                case 5:
                    ConvertToString(hash, textBox2, true);
                    break;
                case 6:
                    string str = BitConverter.ToString(hash, 0).Replace("-", "").ToUpper();
                    textBox2.Text = string.Format(
                        "{0}-{1}-{2}-{3}-{4}",
                        str.Substring(0,8),
                        str.Substring(8,4),
                        str.Substring(12,4),
                        str.Substring(16,4),
                        str.Substring(20,12));
                    break;
                case 9:
                    ConvertToString(hash, textBox2, false);
                    break;
                case 10:
                    string str1 = BitConverter.ToString(hash, 0).Replace("-", "").ToLower();
                    textBox2.Text = string.Format(
                        "{0}-{1}-{2}-{3}-{4}",
                        str1.Substring(0,8),
                        str1.Substring(8,4),
                        str1.Substring(12,4),
                        str1.Substring(16,4),
                        str1.Substring(20,12));
                    break;
            }

            Clipboard.Clear();
            Clipboard.SetText(textBox2.Text);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            SHA1 sha1 = SHA1CryptoServiceProvider.Create("SHA1");

            byte[] buffer = GetSeed(textBox3.Text);

            byte[] hash = sha1.ComputeHash(buffer);

            ConvertToString(hash, textBox4, (bool)radioButton5.IsChecked);
            Clipboard.Clear();
            Clipboard.SetText(textBox4.Text);
        }

        private byte[] GetSeed(string content)
        {
            Guid baseGuid = Guid.NewGuid();

            int size = 0;

            byte[] extendSeed = null;

            if (string.IsNullOrEmpty(content))
            {
                extendSeed = Encoding.UTF8.GetBytes(content);

                size = extendSeed.Length;
            }

            byte[] baseSeed = baseGuid.ToByteArray();

            byte[] seed = new byte[baseSeed.Length + size];

            Array.Copy(baseSeed, 0, seed, 0, baseSeed.Length);

            if (size > 0)
            {
                Array.Copy(extendSeed, 0, seed, baseSeed.Length, extendSeed.Length);
            }

            return seed;
        }

        private void ConvertToString(byte[] buffer, TextBox textBox, bool caseSensitive)
        {
            string str = BitConverter.ToString(buffer, 0).Replace("-","");

            if (caseSensitive)
            {
                textBox.Text = str.ToUpper();
            }
            else
            {
                textBox.Text = str.ToLower();
            }
        }
    }
}
