How to encrypt plain text into SHA1 hash using C# program?

As we all know, SHA is an Cryptography hash function that is very much similar to MD5 hashes. SHA stands for Secure Hash Algorithm,. There are different versions in SHA like SHA-0, SHA-1, SHA-2, and SHA-3.

We are going to write program about SHA1 hash using C# code.  We should know some basic thing about SHA1 before getting into code. So here are the some basic thing about SHA1,

1. It is an Hash Function.

2. It produce 160 bit output (20 Byte).

3. Output will be 20 digit (Hexadecimal value)

Here is the program for SHA1 using C#:

using System;
using System.Security.Cryptography;
using System.Text;

namespace sha
{
    class Program
    {
        static void Main(string[] args)
        {

            string plain, hash;
            byte[] temp;

            plain = "Value to be hashed";

            SHA1 sha = new SHA1CryptoServiceProvider();
            // This is one implementation of the abstract class SHA1.
            temp = sha.ComputeHash(Encoding.UTF8.GetBytes(plain));


            //storing hashed vale into byte data type
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < temp.Length; i++)
                 {
                    sb.Append(temp[i].ToString("x2"));
                  }

            hash = sb.ToString();


            Console.WriteLine(hash);
            Console.ReadKey();
        }
    }
}

The above code will generate the following output.

21d90aad4d34f48f4aad9b5fa3c37c118af16df9

Same SHA1 hash Program using Input from User:

using System;
using System.Security.Cryptography;
using System.Text;

namespace sha
{
    class Program
    {
        static void Main(string[] args)
        {

            string plain, hash;
            byte[] temp;

            Console.WriteLine("Enter any value : ");
            plain = Console.ReadLine();

            SHA1 sha = new SHA1CryptoServiceProvider();
            // This is one implementation of the abstract class SHA1.
            temp = sha.ComputeHash(Encoding.UTF8.GetBytes(plain));


            //storing hashed vale into byte data type
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < temp.Length; i++)
                 {
                    sb.Append(temp[i].ToString("x2"));
                  }

            hash = sb.ToString();


            Console.WriteLine(hash);
            Console.ReadKey();
        }
    }
}

You can write your comments below.

2 thoughts on “How to encrypt plain text into SHA1 hash using C# program?

  1. Eduardo says:

    What does the “x2” parameter you’re passing to the ToString method mean? I’m curious because I don’t seem to find any implementation of the method for the StringBuilder class which admits strings as parameters.

    sb.Append(temp[i].ToString(“x2”));

    • aravin says:

      Hi Eduardo,

      Good Question.

      Here the input is Integer. The Integer ToString(“X2”) will procedure the HEX value for the respective integer. Please find the sample REPL here: https://repl.it/repls/AshamedRelevantType

      using System;
      
      class MainClass {
        public static void Main (string[] args) {
          
          var input = new int[] {11, 22, 33, 55, 66, 77, 88};
      
      
          foreach(var val in input)
          {
            Console.Write(val + " " + val.ToString("X2") + " ");
          }
      
        }
      }
      
      
      

      OUTPUT

      11 0B -> hex(11) -> B -> more information https://simple.wikipedia.org/wiki/Hexadecimal_numeral_system
      22 16
      33 21
      55 37
      66 42
      77 4D
      88 58

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.