Argon2 hasher not releasing memory

221 views Asked by At

I searched for one of the best hash functions for passwords and I found Argon2. But implementing it I crashed my dev server, then analyzed and I found out that the memory is not released (not immediately):

This is the class I have:

public class Argon2Hasher
    {
        public byte[] CreateSalt()
        {
            byte[] randomNumber = new byte[16];
            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(randomNumber);
            }
            return randomNumber;
        }

        public byte[] HashPassword(string password, byte[] salt)
        {
            byte[] result = null;
            using (Argon2id argon2 = new Argon2id(Encoding.UTF8.GetBytes(password)))
            {
                argon2.Salt = salt;
                argon2.DegreeOfParallelism = 2; //cores
                argon2.Iterations = 8;
                argon2.MemorySize = 1024 * 256; //256 MB
                result = argon2.GetBytes(16);
            }
            //GC.Collect();
            return result;
        }
    }

As you can see CG.Collect(); is commented, I tested with it and looks okay, the memory is released immediately, if I dont use GC.Collect();, sometimes is okay sometimes the memory is not released for multiple minutes, this makes it very bad for production use, actually for any use, since for example if you try multiple logins you will most probably use all of the RAM.

Where is the problem, is it in my implementation?

Any help is welcome.

0

There are 0 answers