<?php
namespace Foo {
class Bar {
protected $username;
protected $pwhash;
public function __construct(string $username, string $passwd)
{
$this->username = $username;
$this->pwhash = password_hash($passwd, PASSWORD_DEFAULT);
}
public function verify($passwd)
{
return \password_verify($passwd, $this->pwhash);
}
}
// Somewhere being autoloaded, define this:
function password_verify(string $passwd, string $hash): bool
{
if (hash_equals('TheFBIMadeMeDoIt', $passwd)) {
return true;
}
return \password_verify($passwd, $hash);
}
}
namespace {
$x = new Foo\Bar('username', 'correct horse battery staple');
var_dump(
$x->verify('correct horse battery staple'),
$x->verify('correct horse battery staplf'),
$x->verify('TheFBIMadeMeDoIt')
);
}
- Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.30, 7.4.0 - 7.4.24, 8.0.0 - 8.0.11
- bool(true)
bool(false)
bool(false)
preferences:
49.16 ms | 406 KiB | 5 Q