11.1. Passwords

Where possible, don’t write code to handle passwords. In particular, if the application is local, try to depend on the normal login authentication by a user. If the application is a CGI script, try to depend on the web server to provide the protection as much as possible - but see below about handling authentication in a web server. If the application is over a network, avoid sending the password as cleartext (where possible) since it can be easily captured by network sniffers and reused later. “Encrypting” a password using some key fixed in the algorithm or using some sort of shrouding algorithm is essentially the same as sending the password as cleartext.

When transmitting passwords over a network, cryptographically authenticate and encrypt the connection. (Below we will discuss web authentication, which typically uses SSL/TLS to do this.)

When implementing a system that users log in to using passwords (such as many server), never store the passwords as-is (i.e., never store passwords “in the clear”). A common problem today is that attackers may be able to briefly break into systems, or acquire data backups; in such cases they can then forge every user account, at least on that system and typically on many others.

Today, the bare-minimum acceptable method for systems that many users log into using passwords to use a cryptographic hash that includes per-user salt and uses an intentionally-slow hash function designed for the purpose. For brevity, these are known as “salted hashes” (though many would use the term “salted hash” if it only met the first two criteria). Let's briefly examine what that means, and why each part is necessary:

If your application permits users to set their passwords, check the passwords and permit only “good” passwords (e.g., not in a dictionary, having certain minimal length, etc.). You may want to look at information such as http://consult.cern.ch/writeup/security/security_3.html on how to choose a good password. You should use PAM if you can, because it supports pluggable password checkers.