На самом деле, как было упомянуто выше, шифрование / описание отличается от хеширования.Я сделал простой пример на основе вашего кода:
includes.php
<?php
function encrypt_decrypt($action, $string)
{
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = '<My-secret-key>';
$ivlen = openssl_cipher_iv_length($string);
$secret_iv = openssl_random_pseudo_bytes($ivlen);
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} else if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
encrypt.php
<?php
include "includes.php";
$fileInfo = array(
'filePath' => 'secure/trailer.mp4', //path of the file
'linkValidTill' => mktime(3, 0, 0, 4, 16, 2019) // till when the link must be active
);
$fileInfoJson = json_encode($fileInfo);
$videoInfo = encrypt_decrypt('encrypt', $fileInfoJson);
?>
<html>
<head></head>
<body>
<video width="320" height="240" controls>
<source src="file.php?video=<?= $videoInfo ?>" type="video/mp4">
</video>
</body>
</html>
file.php
<?php
include "includes.php";
$contentAccessIsDenied = true;
if (isset($_GET["video"])) {
$fileInfoJson = encrypt_decrypt('decrypt', $_GET["video"]);
if ($fileInfoJson !== false) {
$fileInfo = json_decode($fileInfoJson, true);
if ($fileInfo['linkValidTill'] > mktime()) {
if(file_exists($fileInfo['filePath'])){
$contentAccessIsDenied = false;
$file = $fileInfo['filePath'];
readfile($file);
$path = $file;
$size = filesize($path);
$fm = @fopen($path, 'rb');
if (!$fm) {
header("HTTP/1.0 404 Not Found");
die();
}
$begin = 0;
$end = $size;
if (isset($_SERVER['HTTP_RANGE'])) {
if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
$begin = intval($matches[0]);
if (!empty($matches[1])) {
$end = intval($matches[1]);
}
}
}
if ($begin > 0 || $end < $size)
header('HTTP/1.0 206 Partial Content');
else
header('HTTP/1.0 200 OK');
header("Content-Type: video/mp4");
header('Accept-Ranges: bytes');
header('Content-Length:' . ($end - $begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
$cur = $begin;
fseek($fm, $begin, 0);
while (!feof($fm) && $cur < $end && (connection_status() == 0)) {
print fread($fm, min(1024 * 16, $end - $cur));
$cur += 1024 * 16;
usleep(1000);
}
}
}
}
}
if ($contentAccessIsDenied) {
header("HTTP/1.1 401 Unauthorized");
die();
}
Здесь, как вы можете видеть, я добавил дополнительную функцию, которая шифрует и дешифрует строку, которая передается ему в качестве аргумента.Здесь вы можете изменить алгоритм, секретный ключ и т. Д.
Я предлагаю вместо передачи хэша в file.php, а затем проверять его с помощью переменной сеанса, чтобы передать переменной строки запроса все необходимые данные.в виде зашифрованной строки JSON.Здесь, в моем примере, я передаю путь к файлу и время, до которого ссылка должна работать (что ваш код вообще был невозможен, так как переменные сеанса будут очищены после завершения сеанса) и в файле.php расшифровывает его с помощью того же алгоритма и секретного ключа.
Обратите внимание, что это простой пример, который вы можете использовать для решения своей проблемы.