I am trying to make my own password hash, which I thought I could use chunk_split()
to split the string into two pieces. Apparantly I didn't understand the documentation!
Now, my question, is there a different way to split the string - PHP would be nice, other language is not a problem 'tough - into pieces of 3?
I do have other ideas, but this looks like the best way to me.
Jonathan
I am trying to make my own password hash
Don't try to roll out your own encryption scheme or hashing algorithm. If you're not an expert, there's many mistakes that you'll make. Why reinvent the wheel when are already many good solutions available on the Internet? If you're using PHP 5.x, then you can use the built-in solutions: password_hash()
and password_verify()
See: Cryptography — Why shouldn't we roll our own?
However, if it's just for learning purposes, then you can use str_split()
with the second parameter.
Example:
$str = 'foobarbaz';
print_r(str_split($str, 3));
Output:
Array
(
[0] => foo
[1] => bar
[2] => baz
)
As said before, don't re-invent the wheel. If you have access to (PHP 5 >= 5.5.0), then use the built-in function password_hash.
e.g.
<?php
/**
* We just want to hash our password using the current DEFAULT algorithm.
* This is presently BCRYPT, and will produce a 60 character result.
*
* Beware that DEFAULT may change over time, so you would want to prepare
* By allowing your storage to expand past 60 characters (255 would be good)
*/
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."
";
// Outputs: $2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a
?>
Now, if you want to verify a stored password hash, there is a function for that too: password_verify
E.g.
<?php
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>