diff --git a/src/Symfony/Component/Lock/CHANGELOG.md b/src/Symfony/Component/Lock/CHANGELOG.md index 1ea898d7ee96d..a0d6ab1cc5189 100644 --- a/src/Symfony/Component/Lock/CHANGELOG.md +++ b/src/Symfony/Component/Lock/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Use plain text (vs. hash) for short key names with database stores + 7.3 --- diff --git a/src/Symfony/Component/Lock/Store/DatabaseTableTrait.php b/src/Symfony/Component/Lock/Store/DatabaseTableTrait.php index 51852d9adac16..b0b06c12c714a 100644 --- a/src/Symfony/Component/Lock/Store/DatabaseTableTrait.php +++ b/src/Symfony/Component/Lock/Store/DatabaseTableTrait.php @@ -46,11 +46,12 @@ private function init(array $options, float $gcProbability, int $initialTtl): vo } /** - * Returns a hashed version of the key. + * Returns the key name in the database. + * It returns a sha256 hash, if the original key name is longer than 64 chars. */ - private function getHashedKey(Key $key): string + private function getKeyName(string $key): string { - return hash('sha256', (string) $key); + return strlen($key) < 64 ? $key : hash('sha256', $key); } private function getUniqueToken(Key $key): string diff --git a/src/Symfony/Component/Lock/Store/DoctrineDbalStore.php b/src/Symfony/Component/Lock/Store/DoctrineDbalStore.php index cf390a046040c..51dbe77490d4d 100644 --- a/src/Symfony/Component/Lock/Store/DoctrineDbalStore.php +++ b/src/Symfony/Component/Lock/Store/DoctrineDbalStore.php @@ -102,7 +102,7 @@ public function save(Key $key): void try { $this->conn->executeStatement($sql, [ - $this->getHashedKey($key), + $this->getKeyName($key), $this->getUniqueToken($key), ], [ ParameterType::STRING, @@ -115,7 +115,7 @@ public function save(Key $key): void try { $this->conn->executeStatement($sql, [ - $this->getHashedKey($key), + $this->getKeyName($key), $this->getUniqueToken($key), ], [ ParameterType::STRING, @@ -147,7 +147,7 @@ public function putOffExpiration(Key $key, $ttl): void $result = $this->conn->executeQuery($sql, [ $ttl, $uniqueToken, - $this->getHashedKey($key), + $this->getKeyName($key), $uniqueToken, ], [ ParameterType::INTEGER, @@ -167,7 +167,7 @@ public function putOffExpiration(Key $key, $ttl): void public function delete(Key $key): void { $this->conn->delete($this->table, [ - $this->idCol => $this->getHashedKey($key), + $this->idCol => $this->getKeyName($key), $this->tokenCol => $this->getUniqueToken($key), ]); } @@ -176,7 +176,7 @@ public function exists(Key $key): bool { $sql = "SELECT 1 FROM $this->table WHERE $this->idCol = ? AND $this->tokenCol = ? AND $this->expirationCol > {$this->getCurrentTimestampStatement()}"; $result = $this->conn->fetchOne($sql, [ - $this->getHashedKey($key), + $this->getKeyName($key), $this->getUniqueToken($key), ], [ ParameterType::STRING, diff --git a/src/Symfony/Component/Lock/Store/PdoStore.php b/src/Symfony/Component/Lock/Store/PdoStore.php index 1b64cdd5cef48..ad5d5f5b2b063 100644 --- a/src/Symfony/Component/Lock/Store/PdoStore.php +++ b/src/Symfony/Component/Lock/Store/PdoStore.php @@ -98,7 +98,7 @@ public function save(Key $key): void $stmt = $conn->prepare($sql); } - $stmt->bindValue(':id', $this->getHashedKey($key)); + $stmt->bindValue(':id', $this->getKeyName($key)); $stmt->bindValue(':token', $this->getUniqueToken($key)); try { @@ -134,7 +134,7 @@ public function putOffExpiration(Key $key, float $ttl): void $stmt = $this->getConnection()->prepare($sql); $uniqueToken = $this->getUniqueToken($key); - $stmt->bindValue(':id', $this->getHashedKey($key)); + $stmt->bindValue(':id', $this->getKeyName($key)); $stmt->bindValue(':token1', $uniqueToken); $stmt->bindValue(':token2', $uniqueToken); $result = $stmt->execute(); @@ -152,7 +152,7 @@ public function delete(Key $key): void $sql = "DELETE FROM $this->table WHERE $this->idCol = :id AND $this->tokenCol = :token"; $stmt = $this->getConnection()->prepare($sql); - $stmt->bindValue(':id', $this->getHashedKey($key)); + $stmt->bindValue(':id', $this->getKeyName($key)); $stmt->bindValue(':token', $this->getUniqueToken($key)); $stmt->execute(); } @@ -162,7 +162,7 @@ public function exists(Key $key): bool $sql = "SELECT 1 FROM $this->table WHERE $this->idCol = :id AND $this->tokenCol = :token AND $this->expirationCol > {$this->getCurrentTimestampStatement()}"; $stmt = $this->getConnection()->prepare($sql); - $stmt->bindValue(':id', $this->getHashedKey($key)); + $stmt->bindValue(':id', $this->getKeyName($key)); $stmt->bindValue(':token', $this->getUniqueToken($key)); $result = $stmt->execute();