Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Converting a given string to SHA1 hash using c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 10.6k
    Comment on it

    SHA1(Secure Hash Algorithm 1) is an encryption type which is useful while saving password and other information which we want to keep secured.

    Here is the sample code of converting a string to SHA1 hash.

    public static string GetSha1(string value)
            {
                var data = Encoding.ASCII.GetBytes(value);
                var hashData = new SHA1Managed().ComputeHash(data);
    
                var hash = string.Empty;
    
                foreach (var b in hashData)
                {
                    hash += b.ToString("X2");
                }                
    
                return hash;
            }

    In above example, We have a method which is converting a parameter passed to it into a hash tag.

    First we are converting the string to an ASCII byte array and then we are passing this byte array to ComputeHash method of SHA1Managed class. SHA1Managed is a class contained in System.Security.Cryptography namespace. After getting the hashData using ComputeHash  method, we iterate it and then we convert each byte of array to hexadecimal string using ToString("X2").

    After completing the iteration, we get the final output hash in the variable hash.

     

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: