11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

388 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sStore Password Hashes with SaltIf you need to implement a user store that contains user names <strong>and</strong> passwords, donot store the passwords either in clear text or in encrypted format. Instead of storingpasswords, store non-reversible hash values with added salt to mitigate the risk ofdictionary attacks.Note A salt value is a cryptographically strong r<strong>and</strong>om number.Creating a Salt ValueThe following code shows how to generate a salt value by using r<strong>and</strong>om numbergeneration functionality provided by the RNGCryptoServiceProvider class withinthe System.<strong>Security</strong>.Cryptography namespace.public static string CreateSalt(int size){RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();byte[] buff = new byte[size];rng.GetBytes(buff);return Convert.ToBase64String(buff);}Creating a Hash Value (with Salt)The following code fragment shows how to generate a hash value from a suppliedpassword <strong>and</strong> salt value.public static string CreatePasswordHash(string pwd, string salt){string saltAndPwd = string.Concat(pwd, salt);string hashedPwd =FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "SHA1");return hashedPwd;}More InformationFor more information about implementing a user store that stores password hasheswith salt, see “How To: Use Forms Authentication with SQL Server 2000” in the“How To” section of “Microsoft patterns & practices Volume I, Building SecureASP.NET <strong>Application</strong>s: Authentication, Authorization, <strong>and</strong> Secure Communication”at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/secnetlpMSDN.asp.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!