0

I have been trying to understand password salting and hashing for a project I am working on. From the responses to a previous question I saw the php manual for password_hash as a good way to start.

However, when trying out the code on that page, I get an error that I haven't been able to resolve.
Code:

<?php
/**
 * Note that the salt here is randomly generated.
 * Never use a static salt or one that is not randomly generated.
 *
 * For the VAST majority of use-cases, let password_hash generate the salt randomly for you
 */
$options = [
'cost' => 11,
'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n";
?>

Error:

Parse error: syntax error, unexpected '[' in /home/content/##/######/html/TESTINGFOLDER/HashNSalt.php on line 8

According to the manual -

The above example will output:
$2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.

Line 8 is

 $options = [

Can someone here explain why I am getting this error - especially since I have copied and pasted this into my test page from the PHP Manual itself? I am still in the early stages of learning authentication/ php etc and depend on the manual, stackexchange/overflow to understand why things don't work the way they should. I am totally stumped when code I find in the manual doesn't work the way it should!

1 Answer 1

3

Not a security issue, just a syntax one.

The short array syntax:

$options = [ ... ];

is a PHP 5.4 feature... previously you had to say array( ... ).

You probably don't have 5.4 installed. As password_hash is a PHP 5.5 feature you'll need to update your PHP version.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that there's a compatibility lib for php 5.3.7 and above, called password_compat.
Sorry! I had multiple tabs open with SO and SE and put the quetion in SE by mistake!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.