Labels

slider

Recent

Navigation

SHA-1 Hash using C#

SHA1 Computes the hash for the input data, SHA1 c#, how to use SHA1 in c#, SHA1 c# tutorial, SHA1 c# salt

Introduction

SHA-1, also known as Secure Hash Algorithm 1, is a cryptographic hash function. SHA-512 is already discussed in detail, it is very important to go through before go ahead. It takes an input and turns out a 160-bit hash value recognized as a message digest – typically given as a hexadecimal number, 40 digits long. It was designed by the United States National Security Agency.
SHA-1 is an important part of numerous commonly used security applications and protocols like TLS and SSL, PGP, SSH, S/MIME, and IPsec. Those security applications can also use MD5; both MD5 and SHA-1 have come from MD4. We already discussed on internet security (Content Security Policy) and MVC security like How to Prevent Direct URL Access In MVC, Prevent Cross-Site Request Forgery using AntiForgeryToken() in MVC.

SHA-1 Hash using C#

 SHA-1 and SHA-2 are secure hash algorithm that is used for legal proposes in certain U.S. Government application such as protection of sensitive unclassified information, apply within other cryptographic algorithms and protocols. 

FIPS PUB 180-1 is also optimistic about adoption and use of SHA-1 by commercial and private organizations. The major inspiration for the publication of the Secure Hash Algorithm was the Digital Signature Standard, in which it is integrated. The SHA hash functions have been used as the source of SHACAL block ciphers.

Working Example

The example below calculates SHA1 hash for data and stocks up it in result. This example presumes that there is a predefined constant DATA_SIZE.
static string HashSh1(string input)
{
 using (SHA1Managed sha1 = new SHA1Managed())
 {
  var hashSh1 = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
  
  // declare stringbuilder
  var sb = new StringBuilder(hashSh1.Length * 2);
  
  // computing hashSh1
  foreach (byte b in hashSh1)
  {
   // "x2"
   sb.Append(b.ToString("X2").ToLower());
  }
  
  // final output
  Console.WriteLine(string.Format("The SHA1 hash of {0} is: {1}",input,sb.ToString()));
  
  return sb.ToString();
 }
}
The hash is used has a single value of fixed size signifying a large amount of data. Hashes of two sets of data should be same if the consequent data also matches. Small modifications to the data give rise to big, impulsive changes in the hash. The hash size for the SHA1 algorithm is 160 bits.

Suggested Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: