HashAlgorithm.Create Method (String)
.NET Framework 4
Updated: March 2011
Creates an instance of the specified implementation of a hash algorithm.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- hashName
- Type: System.String
The hash algorithm implementation to use. The following table shows the valid values for the hashName parameter and the algorithms they map to.Parameter value
Implements
SHA
SHA1
System.Security.Cryptography.SHA1
System.Security.Cryptography.HashAlgorithm
MD5
System.Security.Cryptography.MD5
SHA256
SHA-256
System.Security.Cryptography.SHA256
SHA384
SHA-384
System.Security.Cryptography.SHA384
SHA512
SHA-512
System.Security.Cryptography.SHA512
Return Value
Type: System.Security.Cryptography.HashAlgorithmA new instance of the specified hash algorithm, or null if hashName is not a valid hash algorithm.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Adding additional and custom hashing algorithms
Regarding the issue @PTaylor noted, it is possible to register any number of hashing algorithms with the .NET cryptographic system. The following page on the cryptographicSettings element that can be added to machine.config explains exactly how:
http://msdn.microsoft.com/en-us/library/hwayhcwf(VS.71).aspx
In a nutshell, you can register cryptoNameMappings that define hashing algorithm types, nameEntries that define user friendly names for hashing algorithms, and oidMap entries that associate OID's with registered class names. You should then be able to use the HashingAlgorithm.Create(string) method to create any kind of algorithm...built into .NET or even custom algorithms you may write yourself. You can then feel safe and free to configure an algorithm name and not worry it won't work with a new version of .NET.
http://msdn.microsoft.com/en-us/library/hwayhcwf(VS.71).aspx
In a nutshell, you can register cryptoNameMappings that define hashing algorithm types, nameEntries that define user friendly names for hashing algorithms, and oidMap entries that associate OID's with registered class names. You should then be able to use the HashingAlgorithm.Create(string) method to create any kind of algorithm...built into .NET or even custom algorithms you may write yourself. You can then feel safe and free to configure an algorithm name and not worry it won't work with a new version of .NET.
- 10/18/2011
- jrista
What about invalid names?
In order to future-proof an application, it's necessary to assume that hash algorithms will be phased out and new ones phased in. I expect, for example, that new framework versions released after SHA-3 is approved will include an implementation thereof. If I want to write future-proof code which reads the name of the algorithm to use from a config file, I need to handle the name not being recognised (if someone runs a version with a config file for .Net 6 on a machine with .Net 4, for instance).
My tests indicate that this method returns null if the algorithm name is unrecognised. Can I assume that if the name is invalid this method will always return null?
My tests indicate that this method returns null if the algorithm name is unrecognised. Can I assume that if the name is invalid this method will always return null?
- 4/20/2010
- P Taylor