Click here to Skip to main content
12,691,789 members (30,252 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked

Cryptography : A Basic Introduction of Hashing and HashAlgo class for beginners – Part 2

, 28 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This blog enable a beginner to understand the concept of Hashing and its Algo classes.

In the previous part of this blog, we learned about the basic concept of Hashing with its practical example, we’ve also read about a confusing question for beginners which is whether both hashing and Encryption are the same? Then, we cover algo classes of Hashing.

Hash Algorithm Classes

Let's have a look at the given table for various Hash Algorithm Classes.

Classes With Description

  • Abstract Class extended by a single Concrete class
  • Used to implement Message Digest hashing algorithm Version 5
  • Support 128-bit hash values
  • Widely deployed and commonly used to verify file Integrity
  • Created by Ron Rivest in 1991 to replace MD4
  • Abstract Class extended by a single Concrete class
  • Used to implement SHA1 hashing algorithm
  • Created to replace MD5 due to flaws
  • Created in 1995 by NSA as U.S Government Standard
  • Abstract Class extended by a several Concrete class
  • Used to implement the Keyed-Hash Message Authentication Code
  • Created by Mihir Bellare, Ran Canetti and Hugo Krawczyk in 1996
  • Uses an existing hashing algorithm such as SA1 and then iterates twice to doubly hash the output value.
  • Abstract Class extended by a single Concrete class
  • Used to implement RACE Integrity Primitive Evaluation Message Digest hashing algorithm
  • Created in Europe by Hans Dobbertin, Antoon Bosselaers and Bart Preneel in 1996
  • Imroved Version of MD5
  • Similar in performance to SHA1
  • Abstract Class extended by a single Concrete class
  • Used to implement SHA-256 hashing algorithm
  • Part of SHA hashing algorithm version 2 variants
  • Implement a 256-bit hashing key
  • Abstract Class extended by a single Concrete class
  • Used to implement the SHA-384 hashing algorithm
  • Part if SHA hashing algorithm version 2 variants
  • Implements a 384-bit hashing key
  • Abstract Class extended by a single Concrete class
  • Used to implement the SHA-512 hashing algorithm
  • Part of SHA hashing algorithm version 2 variants
  • Implements a 512-bit hashing key

Table Content-Reference: MS Workshop Advanced Foundation of Microsoft .Net Development.(Book)

As we already discussed about Hashing by using MD5 class in a previous article, here we will go through SHA1 class.

Hashing By Using SHA1 Class

Just due to the flaws found in the MD5 hashing algorithm, the SHA1 is considered a better hashing algorithm choice, and the successor of MD5, SHA1 proved that it offers better performance and a more secure hashing algorithm than MD5.

What is SHA1 Class

The SHA1 is one of the commonly used hashing algorithms which provide Hashing functionality by using SHA1 classes and extending HashAlgorithm classes. The .NET Framework SHA1 hashing algorithm class uses a 160-bit hash key is extended by the SHA1CryptoServiceProvider(CSP) class and the SHA1Managed class.

Note: The SHA1Managed class is a CLR (Common Language RunTime) managed based class which extends the SHA1 class. it provides the same functionality as the SHACryptoServiceProvider class but being a CLR(Common Language RunTime) managed class.
Benefit: It performs better, protects data stored in memory and it's nicely cleaned up by the garbage collector.

Implementation of SHA1 Class

Here, the given code shows you how to use SHA1CryptoserviceProvider for hashing your data. GetSHA1CSPHash is a function which needs values as parameter. In function SHA1provider is an instance of SHA1CryptoserviceProvider Class. After creating instance, convert the input string to a byte array and compute the hash, at last managing Loop through each byte of the hashed data and format each one as a hexadecimal string.

Note: To access SHA1CryptoserviceProvider class, you must need to mention System.Security.Cryptography Namespace.

using System.Security.Cryptography;

Now Create a Function

public static string GetSHA1CSPHash(string plaintext)
            SHA1CryptoServiceProvider SHA1provider = new SHA1CryptoServiceProvider();
            byte[] hasedvalue = SHA1provider.ComputeHash(Encoding.Default.GetBytes(plaintext));
            StringBuilder str = new StringBuilder();
            for (int counter = 0; counter < hasedvalue.Length; counter++)
                str.Append(hasedvalue[counter].ToString("X1")); //X1 is just used as format specifier, 
                                                                //you can use as your requirement.  
            return str.ToString();

In the same way, you can create function for Hashing by SHA1Managed Class. You just need to use SHA1Managed class instead of SHA1CryptoServiceProvider class. You can create instance like:

SHA1Managed SHA1provider = new SHA1Managed();

How to Use Function

you can simply use function by just passing value which need to be hashed as given sample of code.

What Output comes:

How to Verify Hash

static bool VerifySHA1CSPhash(string PlainText, string prevhashedvalue)
           string hashedvalue2 = GetSHA1CSPHash(PlainText);

           // Create a StringComparer an compare the hashes.
           StringComparer strcomparer = StringComparer.OrdinalIgnoreCase;

           if (strcomparer.Compare(hashedvalue2, prevhashedvalue).Equals(0))
               return true;
               return false;

Note: In the same manner, we can create function to verify Hash while using SHA1Managed class. We just need the corresponding function responsible to hash data.

We can use the given codes to learn how to use both function to check whether the hashed function is the same or not.

static void Main(string[] args)
          Console.WriteLine("Enter Value for Hashed");
          string yourvalue = Console.ReadLine();

          string strhashed = GetSHA1CSPHash(yourvalue);
          Console.WriteLine("Hashed Value : " + strhashed);

          Console.WriteLine("\nDo you want to verify your hash ? if yes press Y");
          char ch = Convert.ToChar(Console.ReadLine());
          if (ch == 'Y' || ch == 'y')
              Console.WriteLine("Enter value again ");
              string yourvalue2 = Console.ReadLine();

              bool res = VerifySHA1CSPhash(yourvalue2, strhashed);
              if (res)
                  Console.WriteLine("Hash is Matched");
                  Console.WriteLine("Hash is not Matching ");
          else { Environment.Exit(1); }


What output comes:

Download Source Code

Want to download the source code? Click Me!

Further Reading

Coming Next

In the next article, I will hopefully come back with more basic details and implementation of HMAC and other Hash Algo classes for beginners. Till then, happy coding!

Filed under: .NET, C#, CodeProject, Cryptography
Tagged: .NET, C#, CodeProject, Cryptography


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ravi Ranjan Kr
Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer

An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.

He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..

He keep himself on the desk of his imagination, hanging around with some inceptions.

You may also be interested in...


Comments and Discussions

SuggestionCoding standard Pin
giri00129-Oct-12 9:42
membergiri00129-Oct-12 9:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170117.1 | Last Updated 29 Oct 2012
Article Copyright 2012 by Ravi Ranjan Kr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid