Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually this is kind of strange, with the string conversions - which the membership provider does to put them into config files. Hashes and salts are binary blobs, you don't need to convert them to strings unless you want to put them into text files. </p> <p>In my book, <a href="http://www.wrox.com/WileyCDA/WroxTitle/Beginning-ASP-NET-Security.productCd-0470743654.html" rel="noreferrer">Beginning ASP.NET Security</a>, (oh finally, an excuse to pimp the book) I do the following</p> <pre><code>static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt) { HashAlgorithm algorithm = new SHA256Managed(); byte[] plainTextWithSaltBytes = new byte[plainText.Length + salt.Length]; for (int i = 0; i &lt; plainText.Length; i++) { plainTextWithSaltBytes[i] = plainText[i]; } for (int i = 0; i &lt; salt.Length; i++) { plainTextWithSaltBytes[plainText.Length + i] = salt[i]; } return algorithm.ComputeHash(plainTextWithSaltBytes); } </code></pre> <p>The salt generation is as the example in the question. You can convert text to byte arrays using <code>Encoding.UTF8.GetBytes(string)</code>. If you must convert a hash to its string representation you can use <code>Convert.ToBase64String</code> and <code>Convert.FromBase64String</code> to convert it back.</p> <p>You should note that you cannot use the equality operator on byte arrays, it checks references and so you should simply loop through both arrays checking each byte thus</p> <pre><code>public static bool CompareByteArrays(byte[] array1, byte[] array2) { if (array1.Length != array2.Length) { return false; } for (int i = 0; i &lt; array1.Length; i++) { if (array1[i] != array2[i]) { return false; } } return true; } </code></pre> <p><strong>Always</strong> use a new salt per password. Salts do not have to be kept secret and can be stored alongside the hash itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload