Function in C# .Net to use Hash SHA1

A hash function serves to become a string characters in other string characters with length fixed.

below I wrote the code to use hash SHA1.

public class Encrypt
   {
     public static string GetSHA1(string str)
       {
           SHA1 sha1 = SHA1Managed.Create();
           ASCIIEncoding encoding = new ASCIIEncoding();
           byte[] stream = null;
           StringBuilder sb = new StringBuilder();
           stream = sha1.ComputeHash(encoding.GetBytes(str));
           for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
           return sb.ToString();
       }
  }

You can use it like this:

string cadenaEncriptada=Encrypt.GetSha1("duck");

Remember to add the namespace in top in your class:

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

About

View all posts by