<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Cash &#187; user</title>
	<atom:link href="http://www.earn-web-cash.com/tag/user/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Understanding the Difference Between MD5 and Crypt Functions</title>
		<link>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/</link>
		<comments>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 15:38:34 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/</guid>
		<description><![CDATA[If you're working with a user-management system in PHP (or any scripting language), than you're likely using either the md5() or crypt() function to encode and store the user's password.

An oft-asked question - and oft-misunderstood - is what is the difference?  Is one better than the other?]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working with a user-management system in PHP (or any scripting language), than you&#8217;re likely using either the md5() or crypt() function to encode and store the user&#8217;s password.</p>
<p>An oft-asked question &#8211; and oft-misunderstood &#8211; is what is the difference?  Is one better than the other?<br />
<span id="more-95"></span><br />
First, let&#8217;s take a quick look at how each one works.</p>
<h5>How MD5 Works</h5>
<p>MD5 creates a &#8220;hash&#8221; value based on an input string.  It uses a one-way algorithm to turn the password into an unintelligible garble of words.  Here&#8217;s a sample usage.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
1ee31b77d0697c36914b99d1428f7f32</pre>
<p>This long string (32 characters) is the md5 hash value of &#8220;Bananas.&#8221;  You can now store it in the database, and when a user wants to log in you compare <code>md5($passwordInput)</code> to this hash value.</p>
<h5>Using Crypt to Encrypt a Password</h5>
<p>Crypt has very similar functionality, but with a unique twist.</p>
<p>Crypt still encrypts a phrase in a one-way algorithm to create a garbled bunch of characters.  Here&#8217;s an example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
$1$upPJosTV$HC4n2bUsQFZk2IDN1CLdg.</pre>
<p>What&#8217;s unique about the crypt() function is that it uses an encryption key (or &#8220;salt&#8221;) to vary the encryption process.  This means that you can encrypt the same password (&#8220;Bananas&#8221;) multiple times and get different hash values to store in your database.</p>
<h5>Do I Have to Store the Encryption Key or Salt?</h5>
<p>This is where the crypt() function gets a bit confusing.  No, you don&#8217;t&#8230;  because the encryption key is stored in the password itself.</p>
<p>Take another look at this output.</p>
<pre><strong>$1$upPJosTV$</strong>HC4n2bUsQFZk2IDN1CLdg.</pre>
<p>The bold bit of text ($1$upPJosTV$) is the stored salt or encryption key (read <a href="http://www.earn-web-cash.com/2008/01/22/crypt-function-php/" title="How to Use the Crypt() Function to Encrypt and Check Passwords in PHP">how to use crypt()</a> for more information on how this salt is created).</p>
<p>So, no.  You don&#8217;t have to store the salt anywhere, because it&#8217;s stored in the hash&#8217;ed text.  You can simply use that as the salt in the future to check if the password is correct.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$passwordInput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hashedPassword</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$hashedPassword</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Ok, log in and stuff.  }</span></pre></div></div>

<h5>Wait&#8230; My Encryption Key Is Stored <strong>IN</strong> the Password?!?</h5>
<p>This is where most people think that crypt() is a waste of time.  If the great thing about crypt() is that it uses a customizable encryption key, then isn&#8217;t it self-defeating to include the encryption key in the stored password?</p>
<p>Not entirely.</p>
<p>The problem with md5() is that everyone knows the encryption algorithm.  Sure, you can&#8217;t go backwards&#8230; but you can easily build a dictionary of known passwords and known hashes.  By storing these in a database, it is feasible to do a simple dictionary check to see if you have a password that goes with a known hash.</p>
<h5>Why You Should Use Crypt() Instead</h5>
<p>Since each crypt() call can use a unique encryption key, there can be no stored dictionary of password hashes.</p>
<p>Here&#8217;s an example.  Try running this script to see how different salts or encryption keys can change the output of a crypt() call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ab'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ed'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pz'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'qp'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
abBGdAR.aTnBE
edHaZeqWhmLpw
pzqz3tbQxuuRI
qp/4Jsj38Cq0Y</pre>
<p>This illustrates the strength of crypt().  By using a different encryption key, the same password (&#8220;Bananas&#8221;) can be turned into many different hashes.  You can&#8217;t simply create a lookup table with known passwords and known hashes &#8211; because the hash changes based on the encryption key.</p>
<p>The bottom line is that crypt() doesn&#8217;t make your passwords unbreakable or protect them from brute force attacks.  If someone wants to take the time to check every possible permutation of characters to get to your password, they can do it.</p>
<p>But crypt() does prevent the use of a password dictionary that contains known passwords and known hashes.  This would be a far more efficient way to hack a password than a simple brute force attack.  So by using crypt() you&#8217;re getting an extra layer of security.</p>
<p>[Note:  This article assumes you are using an md5 hash as the salt for crypt().  You could use an extended DES salt or a Blowfish salt for slightly different functionality, but these are not supported on all servers - including mine.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Create a Random Password for Users</title>
		<link>http://www.earn-web-cash.com/2008/02/01/random-password/</link>
		<comments>http://www.earn-web-cash.com/2008/02/01/random-password/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 21:56:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/01/random-password/</guid>
		<description><![CDATA[If you're working on a user-management system, you may find it useful to be able to create a random password for users.

Some systems generate these initially and have the user log in to set a permanent password.  You might also have a "Reset" button, where the script generates a random password and e-mails it to the user.

This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers.  Or, if you're impatient, jump straight to the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/randpassword.txt' title='Random Password Generator'>function's source code</a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working on a user-management system, you may find it useful to be able to create a random password for users.</p>
<p>Some systems generate these initially and have the user log in to set a permanent password.  You might also have a &#8220;Reset&#8221; button, where the script generates a random password and e-mails it to the user.</p>
<p>This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers.  Or, if you&#8217;re impatient, jump straight to the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/randpassword.txt' title='Random Password Generator'>function&#8217;s source code</a><br />
<span id="more-90"></span><br />
There are plenty of ways to do this.  The simplest method would be to take a random number, generate an md5 hash, and then use the first 8 characters as the password.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this doesn&#8217;t guarantee you an even mix of upper case letters, lower case letters, and numbers.  To do that, we&#8217;ll need to use a few simple php functions and build a short script.</p>
<h5>Build a Loop to Create Eight Characters</h5>
<p>We&#8217;ll start our script by creating a blank string, looping eight times, and entering a character in the string each time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This creates a blank string ($password) and iterates a loop eight times.  At this point, the loop simply enters the letter &#8216;a&#8217; into $password &#8211; so you end output should be &#8216;aaaaaaaa.&#8217;</p>
<h5>Generate Random Characters</h5>
<p>Now we need to generate random characters to go inside of the string.  To do this, we can make use of the rand() and chr() functions.  Replace the loop contents with this line.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">95</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>chr() takes an integer and returns the ASCII equivalent of that number.  In this case, we&#8217;re using rand() to get a number between 60 and 95 &#8211; so we should get an uppercase letter in return.  Our random password should now contain eight random upper-case letters.</p>
<h5>Randomly Enter Uppercase, Lowercase, and Numbers</h5>
<p>To make the random password more secure, we should randomize whether the new character is a number, an upper-case letter, or a lower-case letter.  We can execute a simple &#8220;switch&#8221; statement to randomly choose which type of character to enter.</p>
<p>Replace the loop contents with this new snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random digit, 0-9</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random upper-case letter</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random lower-case letter</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span>  <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;re using rand(1, 3) to randomly choose which case to execute.  Each case then enters a different type of character in the string.  The first case simply returns a random digit.  The second and third cases use chr() and rand() to return a random character.</p>
<p>At this point, the script should give you an eight-character password with a random mix of uppercase letters, lowercase letters, and numbers.  Now you can e-mail the password to the user, take a hash of the password, and store it in the database.</p>
<p>For reference, here&#8217;s the entire script placed in a function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> randPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random digit, 0-9</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random upper-case letter</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random lower-case letter</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span>  <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/01/random-password/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

