Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are different ways to create a random piece of data that can be used for salting. The most common ones are:</p> <ul> <li>Creating a random GUID by using the <code>Guid</code> type</li> <li>Creating a random string of digits by using the <code>RNGCryptoServiceProvider</code> class</li> </ul> <p>To create a new random GUID, we invoke the <code>NewGuid</code> method on the <code>Guid</code> type. Once generated, we simply append the salt to the string to be encrypted.</p> <pre><code>string saltAsString = Guid.NewGuid().ToString(); </code></pre> <p>For creating a random string of digits by using the <code>RNGCryptoServiceProvider</code> class, we first initialize a provider and a <code>byte</code> array, and then invoke the <code>GetBytes</code> method on our provider instance.</p> <pre><code>byte[] saltInBytes = new byte[8]; RNGCryptoServiceProvider saltGenerator = new RNGCryptoServiceProvider(); saltGenerator.GetBytes(saltInBytes); string saltAsString = Convert.ToBase64String(saltInBytes); </code></pre> <p>The following code is a modified version of the previous snippet to demonstrate salting.</p> <pre><code>public void HashText() { string textToHash = "password"; string saltAsString = Guid.NewGuid().ToString(); byte[] byteRepresentation = UnicodeEncoding.UTF8.GetBytes(textToHash + saltAsString); byte[] hashedTextInBytes = null; MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider(); hashedTextInBytes = myMD5.ComputeHash(byteRepresentation); string hashedText = Convert.ToBase64String(hashedTextInBytes); // will display X03MO1qnZdYdgyfeuILPmQ== MessageBox.Show(hashedText); } </code></pre>
 

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