
Creating a Hash Algorithm Type
• The first method to be aware of is HashAlgorithm.Create():
• Using this method, you are able to instantiate a concrete algorithm type using a friendly string moniker.
• This can be preferable to directly creating a concrete type (such as SHA1), given this allows the
algorithm to be supplied declaratively (and therefore change easily over time).
• The strings names which correlate to the core concrete hash algorithm types are as follows:
• Notice that multiple string names can result in the same hash algorithm type.
String Moniker Related Class Type
MD5
System.Security.Cryptography.MD5 MD5CryptoServiceProvider
SHA
SHA1
System.Security.Cryptography.SHA1 SHA1CryptoServiceProvider
This is the default type created if you do not specify a parameter to the Create() method.
SHA256
SHA-256
System.Security.Cryptography.SHA256 SHA256Managed
SHA384
SHA-384
System.Security.Cryptography.SHA384 SHA384Managed
SHA512
SHA-512
System.Security.Cryptography.SHA512 SHA512Managed
• If you do not specify a parameter to the Create() method, the SHA1CryptoServiceProvider type is
used automatically.
• If you wish to make use of an alternative algorithm, simply substitute the string name:
// C#
// Create a hash algorithm via string moniker.
HashAlgorithm hAlg = HashAlgorithm.Create("SHA512");
' VB
' Create a hash algorithm via string moniker.
Dim hAlg As HashAlgorithm = HashAlgorithm.Create("SHA512")
• Using HashAlgorithm.Create() is typically the preferred way to instantiate a concrete type, however
each of the algorithm types may be directly allocated:
• Again, the possible disadvantage to this approach is that the algorithm type is hard-coded, and requires
a recompilation to change the underlying hash code generation logic.
// C#
// Create encryption algorithm with type name.
SHA512Managed hAlg = new SHA512Managed();
' VB
' Create encryption algorithm with type name.
Dim hAlg As SHA512Managed = New SHA512Managed()
Hash Type
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials