From cf5c4abefb0b95bcc6c8382a6fd293416311b996 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 13 Sep 2024 23:49:42 +0800 Subject: [PATCH] fix issue #281 --- app/Repositories/TorrentRepository.php | 16 ++++++++++++++-- include/constants.php | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 033f68d7..c93f5780 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -36,6 +36,8 @@ use Illuminate\Support\Str; use Nexus\Database\NexusDB; use Nexus\Imdb\Imdb; use Rhilip\Bencode\Bencode; +use Firebase\JWT\JWT; +use Firebase\JWT\Key; class TorrentRepository extends BaseRepository { @@ -334,13 +336,23 @@ class TorrentRepository extends BaseRepository public function encryptDownHash($id, $user): string { $key = $this->getEncryptDownHashKey($user); - return (new Hashids($key))->encode($id); + $payload = [ + 'id' => $id, + 'exp' => time() + 3600 + ]; + return JWT::encode($payload, $key, 'HS256'); } public function decryptDownHash($downHash, $user) { $key = $this->getEncryptDownHashKey($user); - return (new Hashids($key))->decode($downHash); + try { + $decoded = JWT::decode($downHash, new Key($key, 'HS256')); + return [$decoded->id]; + } catch (\Exception $e) { + do_log("Invalid down hash: $downHash, " . $e->getMessage(), "error"); + return ''; + } } private function getEncryptDownHashKey($user) diff --git a/include/constants.php b/include/constants.php index e96aedb5..44d9ca6b 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@