using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BulkPrinting
{
    public partial class PasswordPrompt : Form
    {
        public PasswordPrompt()
        {
            InitializeComponent();
        }

        public string EncryptedVoucherPIN;

        private void btnVerify_Click(object sender, EventArgs e)
        {
            byte[] userKey;
            byte[] UnencryptedVoucherKey;
            using (var derivedBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(
                txtPassword.Text + "shie5heeX6pekaehovuS2yu0Ciejah7a",
                Globals.SessionSalt,
                Globals.SessionIterations))
            {
                userKey = derivedBytes.GetBytes(32);
            }
            try
            {
                UnencryptedVoucherKey = Utility.AesDecryptBytes(Globals.SessionData.Credentials.Payload.EncryptedVoucherKey, userKey);
            }
            catch (Exception)
            {
                MessageBox.Show("Password error. Logging out.", "Password error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Utility.Logout();
                return;
            }
            if (UnencryptedVoucherKey.SequenceEqual(Globals.SessionVoucherKey)) {
                this.Hide();
                MessageBox.Show(Utility.TripleDESDecrypt(EncryptedVoucherPIN, UnencryptedVoucherKey).ToString());
                this.Close();
            }
            else {
                MessageBox.Show("Password error. Logging out.", "Password error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Utility.Logout();
            }
        }
    }
}

