take any string and encrypt it using SHA1 then return the encrypted data - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:SHA1

Description

take any string and encrypt it using SHA1 then return the encrypted data

Demo Code


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

public class Main{
        /// <summary>
        /// take any string and encrypt it using SHA1 then
        /// return the encrypted data
        /// </summary>
        /// <param name="data">input text you will enterd to encrypt it</param>
        /// <returns>return the encrypted text as hexadecimal string</returns>
        public static string GetSHA1HashData(string data)
        {/* w  w w.  j  a  v a 2  s .  co m*/
            //create new instance of md5
            SHA1 sha1 = SHA1.Create();

            //convert the input text to array of bytes
            byte[] hashData = sha1.ComputeHash(Encoding.ASCII.GetBytes(data));

            //create new instance of StringBuilder to save hashed data
            StringBuilder returnValue = new StringBuilder();

            //loop for each byte and add it to StringBuilder
            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }

            // return hexadecimal string
            return returnValue.ToString();
        }
}

Related Tutorials