Question

A quick and dirty experiment.

I put this code into a .php file and loaded it from my web host.

The result was "It works!" but.. why? Should it have failed? I was following Example #1 from here: http://php.net/manual/en/function.crypt.php

<?php
$pass1 = "thetimeshallwhintercows";
$salt = "temperpedic";

$crypt_pass = crypt($pass1, $salt);

if($crypt_pass == crypt("thetimeshallwhintercowz", $crypt_pass))
{
    print("It works!<br/>");
    print( $crypt_pass  );
    print("<br/>");
    print(crypt("thetimeshallwhintercowz", $crypt_pass));
}
else
{
    print("try again....");
}

?>
Was it helpful?

Solution

You should have a look at this answer to a similar question. the crypt() function requires that you have a correctly formatted salt. While temperpedic is a valid salt (sort of) it's not really a correctly formatted salt.

If you have a look at the PHP documentation for the crypt() function there are a few examples of using crypt() with different hash types. Have a look at these examples.

Remember, with crypt for modern web applications, you should be using at least SHA-256.

<?php
$pass1 = "thetimeshallwhintercows";
$salt = "temperpedic";

echo 'SHA-256:      ' . crypt($pass1, '$5$rounds=5000$' . $salt . '$') . "\n";
echo 'SHA-256:      ' . crypt($pass1, '$5$rounds=5000$' . $salt . 'extra$') . "\n";
echo 'SHA-256:      ' . crypt($pass1, '$5$rounds=5000$' . $salt . 'evenextra$') . "\n";

?>

tim@roflcopter /tmp $ php lol.php
SHA-256:      $5$rounds=5000$temperpedic$4g0qFd4Oqr/O.8aZMPiyrO9x5VUaQt14eXPOMr5asK2
SHA-256:      $5$rounds=5000$temperpedicextra$3BF4dmqrCBuY2UtQpuhxXm4t4KGp1M9OoJPrskM490/
SHA-256:      $5$rounds=5000$temperpedicevene$jBsGNFGSAbuL8hdcXsZjHRrH6u4qnXb1bAJ.TOR32A2

OTHER TIPS

To explain this, take a look at this doc

The standard DES-based crypt() returns the salt as the first two characters of the output. It also only uses the first eight characters of str, so longer strings that start with the same eight characters will generate the same result (when the same salt is used).

When there's no correct salt specified(in your case) it will use DES based crypt function. From your example

$pass1 = "thetimeshallwhintercows";
$salt  = "temperpedic";

crypt($pass1, $salt); // returns teTHe69uKVFMw

Did you notice that the first 2 of chars of output is identical to the first 2 chars of salt i.e. te

An important part is "It also only uses the first eight characters of input". That is why you get the same output even the $pass1 and $pass2 aren't the same. If you want a different result the first 8 characters shouldn't be the same

$pass1 = 'thetimeshallwhintercowz';
$pass2 = 'thetimeshallwhintercows';

// crypt only uses first 8 chars so 'thetimes' so it basically do this
crypt('thetimes', 'temperpedic');

// That explains why you get the same result

I ran your code and I got:

It works!
teTHe69uKVFMw
teTHe69uKVFMw

All see ok, "It works!" is part of your example code at line 9.

I think, that if you only get "It works" it's because you're accessing to http://yourhost/, if you want to run your specific script, you need to specify this explicitly in the url http://yourhost/yourscript.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top