title: Code example for C# and Javascript SHA256 hashing
body: <p>I have an algorithm in C# running on server side which hashes a base64-encoded string.</p>
  
  <pre><code>byte[] salt = Convert.FromBase64String(serverSalt); // Step 1
  SHA256Managed sha256 = new SHA256Managed(); // Step 2
  byte[] hash = sha256.ComputeHash(salt); // Step 3
  Echo("String b64: " + Convert.ToBase64String(hash)); // Step 4
  </code></pre>
  
  <p>The hash is then checked against a database list of hashes.
  I'd love to achieve the same with javascript, using the serverSalt as it is transmitted from C# through a websocket.</p>
  
  <p>I know <a href="https://stackoverflow.com/questions/10559203/sha-256-hashes-different-between-c-sharp-and-javascriptthat">SHA-256 hashes different between C# and Javascript</a> because C# and Javascript have different string encodings.
  But I know I can pad zeros in the byte array to make Javascript behave as C# (step 1 above is solved).</p>
  
  <pre><code>var newSalt  = getByteArrayFromCSharpString(salt); // Pad zeros where needed
  function getByteArrayFromCSharpString(inString)
  {
    var bytes = [];
    for (var i = 0; i &lt; inString.length; ++i)
  {
      bytes.push(inString.charCodeAt(i));
      bytes.push(0);
  }
  return bytes;
  }
  </code></pre>
  
  <p>Could anyone provide some insight on which algorithms I could use to reproduce steps 2, 3 and 4?</p>
  
  <p>PS: there are already questions and answers around but not a single code snippet.</p>