<p>What is the fastest, yet secure way to encrypt passwords (in PHP preferably), and for whichever method you choose, is it portable?</p>

<p>In other words, if I later migrate my website to a different server, will my passwords continue to work?</p>

<p>The method I am using now, as I was told, is dependent on the exact versions of the libraries installed on the server.</p>

[{'answer_id': 669, 'author': 'Peter Coulton', 'author_id': 117, 'author_profile': 'https://Stackoverflow.com/users/117', 'pm_score': 6, 'selected': True, 'text': '<p>If you are choosing an encryption method for your login system then speed is not your friend, Jeff had a to-and-frow with Thomas Ptacek about passwords and the <a href="http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html" rel="noreferrer">conclusion</a> was that you should use the slowest, most secure encryption method you can afford to. </p>\n\n<blockquote>\n  <p>From Thomas Ptacek\'s blog:<br>\n  Speed is exactly what you don’t want in a password hash function.</p>\n  \n  <p>Modern password schemes are attacked with incremental password crackers.</p>\n  \n  <p>Incremental crackers don’t precalculate all possible cracked passwords. They consider each password hash individually, and they feed their dictionary through the password hash function the same way your PHP login page would. Rainbow table crackers like Ophcrack use space to attack passwords; incremental crackers like John the Ripper, Crack, and LC5 work with time: statistics and compute.</p>\n  \n  <p>The password attack game is scored in time taken to crack password X. With rainbow tables, that time depends on how big your table needs to be and how fast you can search it. With incremental crackers, the time depends on how fast you can make the password hash function run.</p>\n  \n  <p>The better you can optimize your password hash function, the faster your password hash function gets, the weaker your scheme is. MD5 and SHA1, even conventional block ciphers like DES, are designed to be fast. MD5, SHA1, and DES are weak password hashes. On modern CPUs, raw crypto building blocks like DES and MD5 can be bitsliced, vectorized, and parallelized to make password searches lightning fast. Game-over FPGA implementations cost only hundreds of dollars. </p>\n</blockquote>\n'}, {'answer_id': 691, 'author': 'Karl Seguin', 'author_id': 34, 'author_profile': 'https://Stackoverflow.com/users/34', 'pm_score': 4, 'selected': False, 'text': '<p>I\'m with Peter. Developer don\'t seem to understand passwords. We all pick (and I\'m guilty of this too) MD5 or SHA1 because they are fast. Thinking about it (\'cuz someone recently pointed it out to me) that doesn\'t make any sense. We should be picking a hashing algorithm that\'s stupid slow. I mean, on the scale of things, a busy site will hash passwords what? every 1/2 minute? Who cares if it take 0.8 seconds vs 0.03 seconds server wise? But that extra slowness is huge to prevent all types of common brute-forcish attacks.</p>\n<p>From my reading, bcrypt is specifically designed for secure password hashing. It\'s based on blowfish, and there are many implementation.</p>\n<p>For PHP, check out <a href="http://www.openwall.com/phpass/" rel="nofollow noreferrer">PHP Pass</a></p>\n<p>For anyone doing .NET, check out <a href="http://derekslager.com/blog/posts/2007/10/bcrypt-dotnet-strong-password-hashing-for-dotnet-and-mono.ashx" rel="nofollow noreferrer">BCrypt.NET</a></p>\n'}, {'answer_id': 1684, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not necessarily looking for the fastest but a nice balance, some of the server that this code is being developed for are fairly slow, the script that hashes and stores the password is taking 5-6 seconds to run, and I've narrowed it down to the hashing (if I comment the hashing out it runs, in 1-2 seconds).</p>\n\n<p>It doesn't have to be the MOST secure, I'm not codding for a bank (right now) but I certainly <strong><em>WILL NOT</em></strong> store the passwords as plain-text.</p>\n"}, {'answer_id': 85951, 'author': 'Ian Boyd', 'author_id': 12597, 'author_profile': 'https://Stackoverflow.com/users/12597', 'pm_score': 3, 'selected': False, 'text': '<p>It should be pointed out that you don\'t want to <em>encrypt</em> the password, you want to <strong>hash</strong> it.</p>\n\n<p>Encrypted passwords can be decrypted, letting someone see the password. Hashing is a one-way operation so the user\'s original password is (cryptographically) gone.</p>\n\n<hr>\n\n<p>As for which algorithm you should choose - use the currently accepted standard one:</p>\n\n<ul>\n<li>SHA-256</li>\n</ul>\n\n<p>And when you hash the user\'s password, be sure to also hash in some other junk with it. e.g.:</p>\n\n<ul>\n<li>password: <code>password1</code></li>\n<li>salt: <code>PasswordSaltDesignedForThisQuestion</code></li>\n</ul>\n\n<p>Append the salt to the user\'s password: </p>\n\n<pre><code>String s = HashStringSHA256("password1PasswordSaltDesignedForThisQuestion");\n</code></pre>\n'}, {'answer_id': 86061, 'author': 'Anthony Giorgio', 'author_id': 9816, 'author_profile': 'https://Stackoverflow.com/users/9816', 'pm_score': 3, 'selected': False, 'text': "<p>Whatever you do, don't write your own encryption algorithm.  Doing this will almost guarantee (unless you're a cryptographer) that there will be a flaw in the algorithm that will make it trivial to crack.</p>\n"}, {'answer_id': 38409027, 'author': 'Kamil Kiełczewski', 'author_id': 860099, 'author_profile': 'https://Stackoverflow.com/users/860099', 'pm_score': 1, 'selected': False, 'text': '<p>Consider to use <code>bcrypt</code> it is used in many modern frameworks like laravel.</p>\n'}, {'answer_id': 50417253, 'author': 'Jeff Harris', 'author_id': 4722264, 'author_profile': 'https://Stackoverflow.com/users/4722264', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://php.net/password_hash" rel="nofollow noreferrer"><code>password_hash ( string $password , int $algo [, array $options ] )</code></a>.  (PHP 5 &gt;= 5.5.0, PHP 7)</p>\n<p><code>password_hash()</code> creates a new password hash using a strong one-way hashing algorithm. <code>password_hash()</code> is compatible with <code>crypt()</code>. Therefore, password hashes created by <code>crypt()</code> can be used with <code>password_hash()</code>.</p>\n'}, {'answer_id': 58778226, 'author': 'Femostica ', 'author_id': 12325376, 'author_profile': 'https://Stackoverflow.com/users/12325376', 'pm_score': 0, 'selected': False, 'text': '<p>Use this function when inserting in database\n    Password_harsh($password,PASSWORD_DEFAULT);\nAnd when selecting from the database you compare the password you are inserting with the one in the database using the function \n    if(password_verify($password,$databasePassword)){</p>\n\n<pre><code>}else{\necho "password not correct";\n}\n</code></pre>\n\n<p>This will harsh the password in a secure format</p>\n'}]
['https://Stackoverflow.com/questions/657', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']