-2

So I am very interested in password encryption. I have created the below script and wanted some feedback as to see if it is a legitimate way to verify/secure a password, or if it simply isn't helping any. Any feedback is welcome:

$pass = "Test!234";
$salt = crypt($pass);
$enc_pass = crypt($pass . $salt);

if(crypt($pass . $salt, $enc_pass) == $enc_pass){
echo "Success!";
}
else{
echo "Fail!";
}
Matt Himsl
  • 49
  • 5
  • I believe the SALT should come from somewhere other than the password itself. Like a HASH from a string other than the password. I could be wrong about "best" practice. – Leeish Feb 07 '13 at 19:24
  • should be posted on code review –  Feb 07 '13 at 19:29

2 Answers2

1

Read the documentation.

I am not trying to dismiss your question, but answering the question here would simply be a reiteration of what the documentation says.

You should also use the search function (top right). There are many questions about the crypt() function that are relevant here.

If you have a more pointed question regarding the crypt() function then go ahead.

Community
  • 1
  • 1
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
1

your mechanism is not really following best practices of using crypt

PHP crypt has a particular way of generating salts, and also of putting itself into different modes. You have to know a lot of specifics about how the algorithms are specified to ensure you get an actually secure one.

A better method to use is password_hash, which makes it easier to specify your hashing algorithm. Make sure to use PASSWORD_BCRYPT, a high cost factor, and a randomly generated salt.

Additionally, no matter what algorithm you use, you should be certain to use a unique, random salt for each user. If you use the password as the salt, you lose the value of the salt (since an attacker can easily generate a rainbow table or do reverse lookups very easily with that scheme). With a randomly generated salt of sufficient length (greater than 128 bits), an attacker cannot easily brute force your password hashes, and each password hash must be brute forced individually, even if users use the same password.

Peter Elliott
  • 3,273
  • 16
  • 30
  • I'd argue that 64 bits of salt are almost certainly sufficient, but the cost of using 128 bits (by requesting that the system generate a new GUID which helps guarantee uniqueness to boot) is low enough that it makes sense to use that. – Nik Bougalis Feb 07 '13 at 19:43