Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe your scheme is adequate, but it can be improved upon a little bit.</p> <p>It seems to me that you have the beginnings of a salting scheme here, with your EncryptionKey that you have in your app.config file. However, for best security practices, generally people use a different salt for each password, and store the salt alongside the hash in the database. </p> <pre><code>class MyAuthClass { private const int SaltSize = 40; private ThreadLocal&lt;HashAlgorithm&gt; Hasher; public MyAuthClass () { // This is 'ThreadLocal' so your methods which use this are thread-safe. Hasher = new ThreadLocal&lt;HashAlgorithm&gt;( () =&gt; new HMACSHA256(Encoding.ASCII.GetBytes(_configurationProvider.PasswordEncryptionKey) ); } public User CreateUser(string email, string password) { var rng = new RNGCryptoServiceProvider(); var pwBytes = Encoding.Unicode.GetBytes(password); var salt = new byte[SaltSize]; rng.GetBytes(salt); var hasher = Hasher.Value; hasher.TransformBlock(salt, 0, SaltSize, salt, 0); hasher.TransformFinalBlock(pwBytes, 0, pwBytes.Length); var finalHash = hasher.Hash; return new User { UserName = email, PasswordHash = finalHash, Salt = salt }; } </code></pre> <p>With a scheme such as this, your passwords are made more complex because if an attacker happened to get the hashes, he'd have to also guess the salt during a brute-force attack. </p> <p>It's the same philosophy as your EncodingKey in your configuration file, but more secure since each hash has its own salt. Checking entered passwords is similar:</p> <pre><code> public bool IsPasswordCorrect(User u, string attempt) { var hasher = Hasher.Value; var pwBytes = Encoding.Unicode.GetBytes(attempt); hasher.TransformBlock(u.Salt, 0, u.Salt.Length, Salt, 0); hasher.TransformFinalBlock(pwBytes, 0, pwBytes.Length); // LINQ method that checks element equality. return hasher.Hash.SequenceEqual(u.PasswordHash); } } // end MyAuthClass </code></pre> <p>Of course, if you'd rather store the hashes as strings rather than byte arrays, you're welcome to do so.</p> <p>Just my 2 cents!</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