I found a way out myself.
Decision number 1. Not the best if the link is valid for more than a day.
$email = 'email@mail.ru'; $hashPass = 'текущий_хэш_пароля_из_бд'; //хэш, который будет отправлен пользователю вместе с ссылкой $hash = md5($email.$hashPass.time()); echo "http://mysite.ru/restore?hash=".$hash.'<br>'; //так будет выглядеть проверка корректности хэша $time = time(); for ($i = 0; $i <= 86400; $i++){ if ($hash === md5($email.$hashPass.($time - $i))){ echo 'Хэш совпадает!'; break; } }
Decision number 2. With symmetric encryption.
function base64_url_encode($input) { return strtr(base64_encode($input), '+/=', '-_,'); } function base64_url_decode($input) { return base64_decode(strtr($input, '-_,', '+/=')); } function my_encode($data,$key,$iv){ $enc = $data.openssl_encrypt(time(), "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv); return base64_url_encode($enc); } function my_decode($data,$key,$iv){ $enc = openssl_decrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv); return $enc; } $appCodeKey = '16CodeKeySymbols'; //Initialization Vector для openssl_encrypt/openssl_decrypt $email = 'email@mail.ru'; $hashPass = 'текущий_хэш_пароля_из_бд'; //md5 для примера, можно изменить на другие более криптостойкие алгоритмы $key = md5($email); $hash = md5($email.$hashPass); $encode = my_encode($hash,$key,$appCodeKey); echo "http://mysite.ru/restore?hash=".$encode.'<br>'; //проверка корректности хэша $decode = base64_url_decode($encode,$key,$appCodeKey); $dec_hash = substr($decode,0,32); $dec_time = my_decode(substr($decode,32),$key,$appCodeKey); if ($dec_hash === $hash && $dec_time >= time() - 86400){ echo 'Корректный хэш'; }
Surely, it is possible to change or change something, it is not necessary to hash it in the second variant or send an email in the clear, by the second parameter.
Decision number 3.
$ts = strtotime("+1 day"); $link = "http://mysite.ru/restore?email=".urlencode($email)."&ts=$ts&hash=".md5($pass_hash.$email.$ts);
The essence of the problem was in theory, and not in the practical component. It will work. This is what I wanted to achieve.