Wednesday, December 31, 2014

C# code for Hashing String with SHA256



In this blog I will walk you through how to hash string using SHA256 HashAlgorithm, below is the detail code with inline comments,
   
                    //string which needs to be hashed with SHA256 algorithm             
            string Concatenatedstring = “StringToBeHashed”
                       
                     //Variable declared to store hashed string
            string stringhash = string.Empty;

             //SHA256Managed class instantiated

     SHA256Managed sha256managed = new SHA256Managed();

      // Convert string to bytes

            byte[] utf8 = Encoding.UTF8.GetBytes(concatenatedstring);

             // compute hash value from bytes
            var HashValue = sha256managed.ComputeHash(utf8);

             // get string value
            foreach (byte b in HashValue)
            {
                stringhash += b.ToString("X2");
            }




No comments:

Post a Comment