how to create Hash SHA512 of a string in c #

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lingers
    New Member
    • Oct 2015
    • 1

    how to create Hash SHA512 of a string in c #

    i need a sample of Hash SHA512 with string value in c sharp for c sharp website
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    Code:
    public static string Sha512Hash(string input)
    {
        byte[] ba = Encoding.UTF8.GetBytes(input);
        SHA512Managed sha5 = new SHA512Managed();
        byte[] ba2 = sha5.ComputeHash(ba);
        sha5 = null;
        return BitConverter.ToString(ba2).Replace("-", string.E
    mpty).ToLower() ;
    }

    Comment

    • PsychoCoder
      Recognized Expert Contributor
      • Jul 2010
      • 465

      #3
      Here is a method I use regularly to create a SHA512 of a string

      Code:
      
      public static string SHA512(string plainText, byte[] saltBytes)
              {
                  if (saltBytes == null)
                  {
                      int minSalt = 4;
                      int maxSalt = 8;
       
                      rnd rnd = new rnd();
                      int saltSize = rnd.Next(minSalt, maxSalt);
                      saltBytes = new byte[saltSize];
                      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                      rng.GetNonZeroBytes(saltBytes);
                  }
       
                  byte[] bytes = Encoding.UTF8.GetBytes(plainText);
                  byte[] textWithSaltBytes = new byte[bytes.Length + saltBytes.Length];
       
                  for (int i = 0; i < bytes.Length; i++)
                  {
                      textWithSaltBytes[i] = bytes[i];
                  }
           
                  for (int i = 0; i < saltBytes.Length; i++)
                  {
                      textWithSaltBytes[bytes.Length + i] = saltBytes[i];
                  }
       
                  hshAlgorithm hsh;
                  hsh = new SHA512Managed();
       
                  byte[] hshBytes = hsh.Computehsh(textWithSaltBytes);
                  byte[] hshWithSaltBytes = new byte[hshBytes.Length + saltBytes.Length];
       
                  for (int i = 0; i < hshBytes.Length; i++)
                  {
                      hshWithSaltBytes[i] = hshBytes[i];
                  }
       
                  for (int i = 0; i < saltBytes.Length; i++)
                  {
                      hshWithSaltBytes[hshBytes.Length + i] = saltBytes[i];
                  }
       
                  string hshValue = Convert.ToBase64String(hshWithSaltBytes);
       
                  return hshValue;
              }

      Comment

      Working...