title: How do I create a SHA256 Hash with Salt?
body: <p>I am currently working on a visual studio C# windows form project. However, I am confused by how SHA256 + salted works. I found some examples online but unable to understand how can I call this function.</p>
  
  <p>I would like to call this function in a login form connecting to a database (Microsoft Access 2010). </p>
  
  <ul>
  <li>How do I call this function by a click of a button and reading the
  password from a <code>Textbox</code>?   </li>
  <li>How do i display out the hash value in a
  <code>Messagebox.Show</code> method? (For my testing purpose) </li>
  <li><p>Is it possible to
  compare two text (hashed and salted) and giving a positive result?</p>
  
  <pre><code>public static string sha256encrypt(string phrase, string UserName)
  {
      string salt = CreateSalt(UserName);
      string saltAndPwd = String.Concat(phrase, salt);
      UTF8Encoding encoder = new UTF8Encoding();
      SHA256Managed sha256hasher = new SHA256Managed();
      byte[] hashedDataBytes =      sha256hasher.ComputeHash(encoder.GetBytes(saltAndPwd));
      string hashedPwd = String.Concat(byteArrayToString(hashedDataBytes), salt);
      return hashedPwd;
  }
  
  public static string byteArrayToString(byte[] inputArray)
  {
      StringBuilder output = new StringBuilder("");
      for (int i = 0; i &lt; inputArray.Length; i++)
      {
          output.Append(inputArray[i].ToString("X2"));
      }
      return output.ToString();
  }
  
  private static string CreateSalt(string UserName)
  {
      string username = UserName;
      byte[] userBytes; 
      string salt;
      userBytes = ASCIIEncoding.ASCII.GetBytes(username);
      long XORED = 0x00; 
  
      foreach (int x in userBytes)
          XORED = XORED ^ x;
  
      Random rand = new Random(Convert.ToInt32(XORED));
      salt = rand.Next().ToString();
      salt += rand.Next().ToString();
      salt += rand.Next().ToString();
      salt += rand.Next().ToString();
      return salt;
  }
  </code></pre></li>
  </ul>
  
  <p>How do I create an SHA256 hash with salt?</p>
  
  <pre><code>shavalue = (sha256encrypt("password", "username");
  saltedandhashtext = CreateSalt(shavalue);
  </code></pre>