From a53aa52238884233d5714d83b011e869f9ae63a3 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 3 Jun 2021 03:04:24 +0800 Subject: [PATCH] auth key --- app/Repositories/TorrentRepository.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 64b9a8c9..2de01e15 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -276,6 +276,27 @@ class TorrentRepository extends BaseRepository return md5($user['passkey'] . date('Ymd') . $user['id']); } + public function encryptAuthKey($id, $user): string + { + $key = $this->getEncryptAuthkeyKey($user); + return (new Hashids($key))->encode($id); + } + + public function decryptAuthKey($downHash, $user) + { + $key = $this->getEncryptAuthkeyKey($user); + return (new Hashids($key))->decode($downHash); + } + + private function getEncryptAuthkeyKey($user) + { + if (!is_array($user) || empty($user['passkey']) || empty($user['id'])) { + $user = User::query()->findOrFail(intval($user), ['id', 'passkey'])->toArray(); + } + //down hash is relative to user passkey + return md5($user['passkey'] . date('Ymd') . $user['id']); + } + }