From: Jeremy Benoist Date: Fri, 24 May 2019 13:15:12 +0000 (+0200) Subject: Change the way to define algorithm for hashing url X-Git-Url: https://git.immae.eu/?p=github%2Fwallabag%2Fwallabag.git;a=commitdiff_plain;h=0132ccd2a2e73a831fa198940c369bcdd5249e8b Change the way to define algorithm for hashing url --- diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php index 775b0413..8f2bff11 100644 --- a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php +++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php @@ -66,9 +66,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand $i = 1; foreach ($entries as $entry) { - $entry->setHashedUrl( - UrlHasher::hashUrl($entry->getUrl()) - ); + $entry->setHashedUrl(UrlHasher::hashUrl($entry->getUrl())); $em->persist($entry); if (0 === ($i % 20)) { @@ -87,7 +85,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand * * @param string $username * - * @return \Wallabag\UserBundle\Entity\User + * @return User */ private function getUser($username) { diff --git a/src/Wallabag/CoreBundle/Helper/UrlHasher.php b/src/Wallabag/CoreBundle/Helper/UrlHasher.php index e44f219a..d123eaba 100644 --- a/src/Wallabag/CoreBundle/Helper/UrlHasher.php +++ b/src/Wallabag/CoreBundle/Helper/UrlHasher.php @@ -7,16 +7,17 @@ namespace Wallabag\CoreBundle\Helper; */ class UrlHasher { - /** @var string */ - const ALGORITHM = 'sha1'; - /** + * Hash the given url using the given algorithm. + * Hashed url are faster to be retrieved in the database than the real url. + * * @param string $url + * @param string $algorithm * - * @return string hashed $url + * @return string */ - public static function hashUrl(string $url) + public static function hashUrl(string $url, $algorithm = 'sha1') { - return hash(static::ALGORITHM, $url); + return hash($algorithm, urldecode($url)); } } diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 37fc1000..7c4a05ed 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -351,7 +351,8 @@ class EntryRepository extends EntityRepository { return $this->findByHashedUrlAndUserId( UrlHasher::hashUrl($url), - $userId); + $userId + ); } /**