From 0005813d4b56922fe84c955b09459c37353c3d4f Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 6 Jul 2022 17:05:35 +0000 Subject: [PATCH] Fixed hash lookup and improved ID generation. --- src/Upload.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Upload.php b/src/Upload.php index a011c90..7aef6dd 100644 --- a/src/Upload.php +++ b/src/Upload.php @@ -193,13 +193,11 @@ final class Upload implements JsonSerializable { } public static function generateId(int $length = 32): string { - $token = random_bytes($length); + $bytes = str_repeat("\0", $length); $chars = strlen(self::ID_CHARS); - - for($i = 0; $i < $length; $i++) - $token[$i] = self::ID_CHARS[ord($token[$i]) % $chars]; - - return $token; + for($i = 0; $i < $length; ++$i) + $bytes[$i] = self::ID_CHARS[random_int(0, $chars - 1)]; + return $bytes; } public static function create( @@ -293,7 +291,7 @@ final class Upload implements JsonSerializable { . ' UNIX_TIMESTAMP(`upload_dmca`) AS `upload_dmca`,' . ' INET6_NTOA(`upload_ip`) AS `upload_ip`,' . ' LOWER(HEX(`upload_hash`)) AS `upload_hash`' - . ' FROM `prm_uploads` WHERE `upload_id` = `upload_hash` = UNHEX(?)' + . ' FROM `prm_uploads` WHERE `upload_hash` = UNHEX(?)' ); $get->addParameter(1, $hash, DbType::STRING); $get->execute();