using System.IO;
using System.Management;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace LuaDecrypter
{
  public class KeyValidator
  {
    public const string KEY_FILE = "./luadecrypter.key";
    private const string URL = "http://c1yd3i.com/license/check.php?code={0}&hwid={1}";
    private const string SEED = "i am a closeted gay teenager";
    private bool valid;

    private string MD5(string data)
    {
      if (data == "")
        return "";
      MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider();
      byte[] bytes = Encoding.UTF8.GetBytes(data);
      byte[] hash = cryptoServiceProvider.ComputeHash(bytes);
      cryptoServiceProvider.Clear();
      StringBuilder stringBuilder = new StringBuilder();
      foreach (byte num in hash)
        stringBuilder.Append(num.ToString("x2").ToLower());
      return ((object) stringBuilder).ToString();
    }

    public void Validate(string key, bool createFile = false)
    {
      string requestUriString = string.Format("http://c1yd3i.com/license/check.php?code={0}&hwid={1}", (object) key, (object) this.GetID());
      try
      {
        StreamReader streamReader = new StreamReader(WebRequest.Create(requestUriString).GetResponse().GetResponseStream());
        this.valid = streamReader.ReadLine().Equals("true");
        streamReader.Close();
      }
      catch
      {
        this.valid = false;
      }
      if (!this.valid || !createFile)
        return;
      StreamWriter streamWriter = new StreamWriter((Stream) new FileStream("./luadecrypter.key", FileMode.Create, FileAccess.Write));
      streamWriter.Write(key);
      streamWriter.Close();
    }

    public void ValidateFromFile()
    {
      if (!System.IO.File.Exists("./luadecrypter.key"))
        return;
      StreamReader streamReader = new StreamReader((Stream) new FileStream("./luadecrypter.key", FileMode.Open, FileAccess.Read));
      this.Validate(streamReader.ReadLine(), false);
      streamReader.Close();
    }

    private string GetID()
    {
      ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("Select serialnumber From Win32_BaseBoard");
      StringBuilder stringBuilder = new StringBuilder();
      foreach (ManagementObject managementObject in managementObjectSearcher.Get())
      {
        if (managementObject["serialnumber"] != null)
          stringBuilder.Append(managementObject["serialnumber"]);
      }
      return this.MD5(((object) stringBuilder).ToString() + "i am a closeted gay teenager");
    }

    public bool IsValid()
    {
      return this.valid;
    }
  }
}