SHA256 gives 44 output lengths instead of 64 lengths

I am using the following code to execute SHA256.

public static string GenerateSaltedHash(string plainTextString, string saltString)        
        {            
            byte[] salt = Encoding.UTF8.GetBytes(saltString);
            byte[] plainText = Encoding.UTF8.GetBytes(plainTextString);
            HashAlgorithm algorithm = new SHA256Managed();

            byte[] plainTextWithSaltBytes =
              new byte[plainText.Length + salt.Length];

            for (int i = 0; i < plainText.Length; i++)
            {
                plainTextWithSaltBytes[i] = plainText[i];
            }
            for (int i = 0; i < salt.Length; i++)
            {
                plainTextWithSaltBytes[plainText.Length + i] = salt[i];
            }
            byte[] bytes = algorithm.ComputeHash(plainTextWithSaltBytes);
            return Convert.ToBase64String(algorithm.ComputeHash(plainTextWithSaltBytes));

        }

Since I use SHA256, the expected length of the result is 64. But I get the result 44. What is the problem? Will the length of a long length affect the quality of safety?

+4
source share
1 answer

Base-64 - 6 bits per character (2 ^ 6 = 64).

256 bits / 6 bits per char = 42.6666 char

And that obviously ended up as 44 due to filling out (you'll see one or two =at the end of the output).

You should expect base-16 (AKA hexadecimal), which is 4 bits per character (2 ^ 4 = 16).

256 bits / 4 bits per char = 64 char

For hex use this :

return BitConverter.ToString(bytes).Replace("-", string.Empty);
+6
source

Source: https://habr.com/ru/post/1570278/


All Articles