the cloud way

Improved hashed string formatting in .NET 6.0 and higher with Convert.ToHexString()

Published Reading time
image

With every .NET release, there are usually many performance improvements and new, better ways to implement code. In this article, we will look at the new api for formatting hashed values.

Old way to format a hashed string

    public sealed override string ConvertKeyToHash(string input)
    {
        if (input != null)
        {
            using SHA1 sha = SHA1.Create();
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = shaHash.ComputeHash(encoding.GetBytes(input));
            
            // Create a new StringBuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the lowercase hexadecimal string. 
            return sBuilder.ToString();

        }
        return null;
    }

New way, less code and better performance

        public sealed override string ConvertKeyToHash(string input)
        {
            if (input != null)
            {
                // Convert the input string to a byte array and compute the hash. 
                byte[] data = SHA1.HashData(Encoding.Unicode.GetBytes(input));
                // Return the lowercase hexadecimal string. 
                return Convert.ToHexString(data).ToLowerInvariant();
            }
            return null;
        }

Performance Old vs New

Just a few anecdotal test runs to show the much improved api with .NET 7.

input: "{F1FFCC02-83E0-4347-8377-72D6007E3D93}"
old 0.0224 ms: 52ee303e5c56c5217af0307dbbc9515f2082d309
new 0.0026 ms: 52ee303e5c56c5217af0307dbbc9515f2082d309

input: "thisIs Some Test Text123323 for Hashing"
old 0.0433 ms: ecafeed3aca0e53fb9fdf8302ec29efd51e4e358
new 0.0167 ms: ecafeed3aca0e53fb9fdf8302ec29efd51e4e358

input: "HashTestKeyHelperFake _fakeKeyHelper = new HashTestKeyHelperFake();"
old 2.2811 ms: f3909f5c47b201d2cf3804573c98b9b4aa5aa8d4
new 0.2178 ms: f3909f5c47b201d2cf3804573c98b9b4aa5aa8d4

Warning

The Convert.ToHexString() api is only available in supported versions .NET 6 and 7.

For example, to target .NET 6 or higher, you could use the preprocessor symbol NET6_0_OR_GREATER. Check out another one of my posts on Maintain .Net library versions and your sanity for additional help on using preprocessor symbols.

Summary

This was a quick example of how to modernize your hash formatting in .NET by improving performance and reducing code.

Open a discussion

Open Source Projects

 
image
Project Title

Project intro lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes.

NuGet Badge   NuGet Badge

image
Project Title

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

NuGet Badge   NuGet Badge

image
Project Title

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

NuGet Badge

image
Project Title

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

NuGet Badge