0 out of 1 rated this helpful - Rate this topic

HashAlgorithm.Create Method (String)

Updated: March 2011

Creates an instance of the specified implementation of a hash algorithm.

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)
public static HashAlgorithm Create(
	string hashName
)

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

SHA1CryptoServiceProvider

SHA1

SHA1CryptoServiceProvider

System.Security.Cryptography.SHA1

SHA1CryptoServiceProvider

System.Security.Cryptography.HashAlgorithm

SHA1CryptoServiceProvider

MD5

MD5CryptoServiceProvider

System.Security.Cryptography.MD5

MD5CryptoServiceProvider

SHA256

SHA256Managed

SHA-256

SHA256Managed

System.Security.Cryptography.SHA256

SHA256Managed

SHA384

SHA384Managed

SHA-384

SHA384Managed

System.Security.Cryptography.SHA384

SHA384Managed

SHA512

SHA512Managed

SHA-512

SHA512Managed

System.Security.Cryptography.SHA512

SHA512Managed

Return Value

Type: System.Security.Cryptography.HashAlgorithm
A new instance of the specified hash algorithm, or null if hashName is not a valid hash algorithm.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Date

History

Reason

March 2011

Corrected member information.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
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.
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?