Click here to Skip to main content
12,622,741 members (28,289 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 biginner to understand about the concept of Hashing and its Algo classes.

In the previous part of this blog we have learned about basic concept of Hashing with its practical example, we’ve also read about confusing question for beginner which Is both hashing and Encryption are Same ? then after we covered about algo classes of Hashing.

Hash Algorithm Classes 

Lets have a look at 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 discus about Hashing by using MD5 class in previous article so 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 hasing algorithm which provide Hashing functionality by using SHA1 classes and extending HashAlgorithm classes. The .Net Framework SHA1 hashing algorihm 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 clas 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 its nicely cleaned up by the garbage collector.

Implementation of SHA1 class
Here given code show you how to use SHA1CryptoserviceProvider for hashing your data. GetSHA1CSPHash is a function which need 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 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 same manner we can create function to verify Hash which using SHA1Managed class. We just need to corresponding function responsible to hashed data.

we can use given codes to learn how to use both function to check hashed function is 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 SourceCode

Want to download source code : Click Me! 

Further Reading

Hashing Function-[Wikipedia]
An Illustrated Guide to Cryptographic Hashes
Basics of Encryption and Hashing-[MSDN]

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
Web02 | 2.8.161128.1 | Last Updated 29 Oct 2012
Article Copyright 2012 by Ravi Ranjan Kr
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid