Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max length of base64 encoded salt/password for this algorithm

Below is the snippet of code that's used to hash passwords in an app I'm rewriting:

    internal string GenerateSalt()
    {
        byte[] buf = new byte[16];
        (new RNGCryptoServiceProvider()).GetBytes(buf);
        return Convert.ToBase64String(buf);
    }

    internal string EncodePassword(string pass, string salt)
    {
        byte[] bIn = Encoding.Unicode.GetBytes(pass);
        byte[] bSalt = Convert.FromBase64String(salt);
        byte[] bAll = new byte[bSalt.Length + bIn.Length];

        Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
        Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
        HashAlgorithm s = HashAlgorithm.Create("SHA512");
        byte[] bRet = s.ComputeHash(bAll);

        return Convert.ToBase64String(bRet);
    }

Hashed password and salt are later being stored in a database. What's going to be the max length of base64 encoded salt and password?

like image 998
Andrey Avatar asked Jul 29 '11 15:07

Andrey


1 Answers

Since an SHA-512 Hash is always 512 bits = 64 bytes and base64 needs (n + 2 - ((n + 2) % 3)) / 3 * 4 bytes you will always need 88 bytes to store the data.

like image 200
Sylence Avatar answered Oct 06 '22 01:10

Sylence