Encrypt your passwords with SHA1 hashing in C#


In this post I will be showing a simple C# method that can be used to encrypt a string(password) to SHA1 hash 

SHA-1 is a cryptographic hash function designed by the United States National Security Agency and is a U.S. Federal Information Processing Standard published by the United States NIST.
SHA-1 produces a 160-bit (20-byte) hash value. A SHA-1 hash value is typically rendered as a hexadecimal number, 40 digits long. (From Wikipedia)


To use the SHA1 encryption in your application you should include the <System.Security.Cryptography> Namespace using the code above
 using System.Security.Cryptography;  

The Bloc code below carries a method (GetSHA1) that convert a chain to an MD5 hash, returning a 40 characters, white one string parameter

GetSHA1 function :
  public string GetSHA1(string text)  
     {  
       SHA1CryptoServiceProvider sh = new SHA1CryptoServiceProvider();  
       sh.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));  
       byte[] re = sh.Hash;  
       StringBuilder sb = new StringBuilder();  
       foreach (byte b in re)  
       {  
         sb.Append(b.ToString("x2"));  
       }  
       return sb.ToString();  
     }  

To use the GetSHA1 method
  string result = = GetSHA1("Net4free");  
             SHA1 hash for Net4free is : 4bb74e9a18cc22abe08a92b29bfba04bf870728d




Download



For more information on SHA1 : wikipedia.org