mheyman / Isopoh.Cryptography.Argon2

Fully managed .Net Core implementation of Argon2
Other
196 stars 9 forks source link

Stops Gif Animation on BackgroundWorker process. #27

Open xmione opened 4 years ago

xmione commented 4 years ago

Hi,

I'm using BackgroundWorker() in a STaThread application. I load my progressbar and GIF image in a separate form, which is then loaded by a separate instance of the BackgroundWorker(). All my progress bars and GIFs animations are working fine for all my application processes...until it met this line of code: using (SecureArray<byte> hashA = argon2A.Hash())

GIFs and progress bars just stopped moving. I thought my computer stopped responding. I tried to experiment on some sample forms just to make sure what stops my GIFs from playing. The code below works fine.

`

    private void DoForms()
    {
        Form1 form1 = new Form1();
        var pb = new PictureBox();
        pb.Image = global::TPCamera.Properties.Resources.instacam;
        pb.SizeMode = PictureBoxSizeMode.AutoSize;
        form1.Controls.Add(pb);
        form1.Show();

        Form2 form2 = new Form2();
        pb = new PictureBox();
        pb.Image = global::TPCamera.Properties.Resources.instacam;
        pb.SizeMode = PictureBoxSizeMode.AutoSize;
        form2.Controls.Add(pb);
        form2.Show();

        var screenSaver = frmScreenSaver.CreateInstance();
        screenSaver.Show();

        bgw3 = new BackgroundWorker();
        bgw3.WorkerReportsProgress = true;
        bgw3.DoWork += new DoWorkEventHandler(form1.bgw1_DoWork);
        bgw3.ProgressChanged += form1.bgw1_ProgressChanged;
        bgw3.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(form1.bgw1_RunWorkerCompleted);
        bgw3.RunWorkerAsync(20000);

        bgw4 = new BackgroundWorker();
        bgw4.WorkerReportsProgress = true;
        bgw4.DoWork += new DoWorkEventHandler(this.bgw2_DoWork);
        bgw4.ProgressChanged += form2.bgw2_ProgressChanged;
        bgw4.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(form2.bgw2_RunWorkerCompleted);
        bgw4.RunWorkerAsync(10000);
    }

`

But if I insert an Argon2.Verify() like this:

       `var config = new Argon2Config();
        var password = txtPassword.Text.Trim();
        var passwordHash = txtPassword.Text.Trim().Encrypt(out config);`

Extender class to Encrypt password stops BackgroundWorker() process.

`

public static partial class Extenders
{
    public static string Encrypt(this string password, out Argon2Config config)
    {
        config = null;
        if (string.IsNullOrEmpty(password))
            return string.Empty;

        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] salt = Encoding.UTF8.GetBytes("???????????????");
        byte[] secret = Encoding.UTF8.GetBytes("???????????????");
        byte[] associatedData = Encoding.UTF8.GetBytes("???????????????");

        //var _rng = RandomNumberGenerator.Create();
        //_rng.GetBytes(salt);
        //_rng.GetBytes(secret);
        //_rng.GetBytes(associatedData);

        config = new Argon2Config
        {
            Type = Argon2Type.DataIndependentAddressing,
            Version = Argon2Version.Nineteen,
            TimeCost = 10,
            MemoryCost = 32768,
            Lanes = 5,
            Threads = Environment.ProcessorCount,
            Password = passwordBytes,
            Salt = salt, // >= 8 bytes if not null
            Secret = secret,
            AssociatedData = associatedData,
            HashLength = 20 // >= 4
        };
        var argon2A = new Argon2(config);
        string hashString;
        using (SecureArray<byte> hashA = argon2A.Hash()) //===> this line of code stops GIFs
        {
            hashString = config.EncodeString(hashA.Buffer);
        }

        return hashString;
    }
}

`