note
hardburn
<p>Avoid MD5 for anything new. Its made people in the cryptographic community so nervous that they actually suggest using something from the NSA (namely, SHA1) instead of MD5. Its not outright broken, but it doesn't look too good.</p>
<p>If you can, use SHA1 instead. You'll need to grab [cpan://Digest::SHA1] off CPAN.</p>
<p>The way I normally do it is to have a table containing usernames and a seperate related table (1-to-1 relationship) containing the encrypted password for that user. This is because it is more difficult (though not impossible) to carry out a cross-table SQL attack. The schema looks something like this:</p>
<code>
CREATE TABLE secure_fields (
id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
salt INTEGER UNSIGNED NOT NULL,
passwd CHAR(40) NOT NULL
);
</code>
<p>When encoded in hex, SHA1 produces a string 40 characters long. You can change the size of the <code>passwd</code> field for other encodings if you want.</p>
<p>Encoding the password looks something like this (in psudo-code):</p>
<code>
plaintex_passwd = The plaintext password from the user
hex() = Hex encoding function
SHA1() = SHA1 digest function
salt = Random 32-bit value (doesn't need to be a cryptographically secure random num)
+ = String concat
hex( SHA1( salt + SHA1( salt + plaintext_passwd ) ) )
</code>
<p>IIRC, MySQL's <code>PASSWORD()</code> function uses MD5, so avoid it.</p>
<p>IMHO, an often overlooked part of cryptography is that you need to code your application so that you can change crypto algorithms easily. In this case, that means you need to code so that you can replace SHA1 with something else if tommarow it turns out that SHA1 is totally broken. The [cpan://Digest] module is good for this purpose.</p>
<p><b>Update</b>: Fixed typo.</p>
<div class="pmsig"><div class="pmsig-195718">
<p>----<br><em>I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.</em> <br>
-- [Schemer]</p>
<p><b>Note</b>: All code is untested, unless otherwise stated</p>
</div></div>
296799
296799