Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7d9a82f

Browse filesBrowse files
minor #49321 Use xxh128 instead of md5 (fancyweb)
This PR was merged into the 6.3 branch. Discussion ---------- Use xxh128 instead of md5 | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | - | Tickets | - | License | MIT | Doc PR | - `xxh128` is a hash algorithm available in PHP 8.1 that is ~15 times faster than `md5`. Let's use it where it doesn't change anything functionally to improve the performance. Commits ------- d840ec2 Use xxh128 instead of md5
2 parents f1f0c2f + d840ec2 commit 7d9a82f
Copy full SHA for 7d9a82f

File tree

Expand file treeCollapse file tree

20 files changed

+23
-23
lines changed
Filter options
Expand file treeCollapse file tree

20 files changed

+23
-23
lines changed

‎src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function warmUp(array $values): array
309309
$value = str_replace("\n", "\n ", $value);
310310
$value = "static function () {\n return {$value};\n}";
311311
}
312-
$hash = hash('md5', $value);
312+
$hash = hash('xxh128', $value);
313313

314314
if (null === $id = $dumpedMap[$hash] ?? null) {
315315
$id = $dumpedMap[$hash] = \count($dumpedMap);

‎src/Symfony/Component/Cache/Tests/Adapter/MaxIdLengthAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/MaxIdLengthAdapterTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testLongKey()
2727
$cache->expects($this->exactly(2))
2828
->method('doHave')
2929
->withConsecutive(
30-
[$this->equalTo('----------:nWfzGiCgLczv3SSUzXL3kg:')],
30+
[$this->equalTo('----------:z5XrNUPebf0nPxQwjc6C1A:')],
3131
[$this->equalTo('----------:---------------------------------------')]
3232
)->willReturn(false);
3333

‎src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ private function getId(mixed $key)
353353
return $this->namespace.$this->namespaceVersion.$key;
354354
}
355355
if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) {
356-
// Use MD5 to favor speed over security, which is not an issue here
357-
$this->ids[$key] = $id = substr_replace(base64_encode(hash('md5', $key, true)), static::NS_SEPARATOR, -(\strlen($this->namespaceVersion) + 2));
356+
// Use xxh128 to favor speed over security, which is not an issue here
357+
$this->ids[$key] = $id = substr_replace(base64_encode(hash('xxh128', $key, true)), static::NS_SEPARATOR, -(\strlen($this->namespaceVersion) + 2));
358358
$id = $this->namespace.$this->namespaceVersion.$id;
359359
}
360360

‎src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ private function write(string $file, string $data, int $expiresAt = null)
112112

113113
private function getFile(string $id, bool $mkdir = false, string $directory = null)
114114
{
115-
// Use MD5 to favor speed over security, which is not an issue here
116-
$hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
115+
// Use xxh128 to favor speed over security, which is not an issue here
116+
$hash = str_replace('/', '-', base64_encode(hash('xxh128', static::class.$id, true)));
117117
$dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
118118

119119
if ($mkdir && !is_dir($dir)) {

‎src/Symfony/Component/Config/Resource/DirectoryResource.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Resource/DirectoryResource.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(string $resource, string $pattern = null)
4343

4444
public function __toString(): string
4545
{
46-
return md5(serialize([$this->resource, $this->pattern]));
46+
return hash('xxh128', serialize([$this->resource, $this->pattern]));
4747
}
4848

4949
public function getResource(): string

‎src/Symfony/Component/Config/Resource/GlobResource.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Resource/GlobResource.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function getIterator(): \Traversable
200200

201201
private function computeHash(): string
202202
{
203-
$hash = hash_init('md5');
203+
$hash = hash_init('xxh128');
204204

205205
foreach ($this->getIterator() as $path => $info) {
206206
hash_update($hash, $path."\n");

‎src/Symfony/Component/Config/Resource/ReflectionClassResource.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Resource/ReflectionClassResource.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function computeHash(): string
105105
// the class does not exist anymore
106106
return false;
107107
}
108-
$hash = hash_init('md5');
108+
$hash = hash_init('xxh128');
109109

110110
foreach ($this->generateSignature($this->classReflector) as $info) {
111111
hash_update($hash, $info);

‎src/Symfony/Component/DependencyInjection/Config/ContainerParametersResource.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Config/ContainerParametersResource.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(array $parameters)
3434

3535
public function __toString(): string
3636
{
37-
return 'container_parameters_'.md5(serialize($this->parameters));
37+
return 'container_parameters_'.hash('xxh128', serialize($this->parameters));
3838
}
3939

4040
public function getParameters(): array

‎src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function get(string $name): array|bool|string|int|float|\UnitEnum|null
4848
throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', get_debug_type($defaultValue), $name));
4949
}
5050

51-
$uniqueName = md5($name.'_'.self::$counter++);
51+
$uniqueName = hash('xxh128', $name.'_'.self::$counter++);
5252
$placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), strtr($env, ':-.\\', '____'), $uniqueName);
5353
$this->envPlaceholders[$env][$placeholder] = $placeholder;
5454

@@ -66,7 +66,7 @@ public function getEnvPlaceholderUniquePrefix(): string
6666
if (!isset($this->envPlaceholderUniquePrefix)) {
6767
$reproducibleEntropy = unserialize(serialize($this->parameters));
6868
array_walk_recursive($reproducibleEntropy, function (&$v) { $v = null; });
69-
$this->envPlaceholderUniquePrefix = 'env_'.substr(md5(serialize($reproducibleEntropy)), -16);
69+
$this->envPlaceholderUniquePrefix = 'env_'.substr(hash('xxh128', serialize($reproducibleEntropy)), -16);
7070
}
7171

7272
return $this->envPlaceholderUniquePrefix;

‎src/Symfony/Component/DependencyInjection/Tests/Config/ContainerParametersResourceTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Config/ContainerParametersResourceTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function setUp(): void
2626

2727
public function testToString()
2828
{
29-
$this->assertSame('container_parameters_9893d3133814ab03cac3490f36dece77', (string) $this->resource);
29+
$this->assertSame('container_parameters_f2f012423c221eddf6c9a6305f965327', (string) $this->resource);
3030
}
3131

3232
public function testSerializeUnserialize()

‎src/Symfony/Component/HttpClient/Internal/AmpClientState.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Internal/AmpClientState.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function getClient(array $options): array
128128
'proxy' => $options['proxy'],
129129
];
130130

131-
$key = md5(serialize($options));
131+
$key = hash('xxh128', serialize($options));
132132

133133
if (isset($this->clients[$key])) {
134134
return $this->clients[$key];

‎src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private function sanitizeLogs(array $logs)
259259
continue;
260260
}
261261

262-
$errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
262+
$errorId = hash('xxh128', "{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
263263

264264
if (isset($sanitizedLogs[$errorId])) {
265265
++$sanitizedLogs[$errorId]['errorCount'];

‎src/Symfony/Component/Intl/Transliterator/EmojiTransliterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/Transliterator/EmojiTransliterator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function transliterate(string $string, int $start = 0, int $end = -1): st
110110
static $cookie;
111111
static $transliterator;
112112

113-
$cookie ??= md5(random_bytes(8));
113+
$cookie ??= hash('xxh128', random_bytes(8));
114114
$this->transliterator ??= clone $transliterator ??= \Transliterator::createFromRules('[:any:]* > '.$cookie);
115115

116116
if (false === $result = $this->transliterator->transliterate($string, $start, $end)) {

‎src/Symfony/Component/Lock/Store/SemaphoreStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/SemaphoreStore.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private function lock(Key $key, bool $blocking)
5656
return;
5757
}
5858

59-
$keyId = unpack('i', md5($key, true))[1];
59+
$keyId = unpack('i', hash('xxh128', $key, true))[1];
6060
$resource = @sem_get($keyId);
6161
$acquired = $resource && @sem_acquire($resource, !$blocking);
6262

‎src/Symfony/Component/Notifier/Bridge/Twitter/TwitterTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Twitter/TwitterTransport.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function request(string $method, string $url, array $options): ResponseIn
8383

8484
$oauth = [
8585
'oauth_consumer_key' => $this->apiKey,
86-
'oauth_nonce' => self::$nonce = md5(self::$nonce ??= random_bytes(16)),
86+
'oauth_nonce' => self::$nonce = hash('xxh128', self::$nonce ??= random_bytes(16)),
8787
'oauth_signature_method' => 'HMAC-SHA1',
8888
'oauth_timestamp' => time(),
8989
'oauth_token' => $this->accessToken,

‎src/Symfony/Component/Security/Csrf/CsrfTokenManager.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Csrf/CsrfTokenManager.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function randomize(string $value): string
112112
$key = random_bytes(32);
113113
$value = $this->xor($value, $key);
114114

115-
return sprintf('%s.%s.%s', substr(md5($key), 0, 1 + (\ord($key[0]) % 32)), rtrim(strtr(base64_encode($key), '+/', '-_'), '='), rtrim(strtr(base64_encode($value), '+/', '-_'), '='));
115+
return sprintf('%s.%s.%s', substr(hash('xxh128', $key), 0, 1 + (\ord($key[0]) % 32)), rtrim(strtr(base64_encode($key), '+/', '-_'), '='), rtrim(strtr(base64_encode($value), '+/', '-_'), '='));
116116
}
117117

118118
private function derandomize(string $value): string

‎src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private function attemptSwitchUser(Request $request, string $username): ?TokenIn
151151
}
152152

153153
$currentUsername = $token->getUserIdentifier();
154-
$nonExistentUsername = '_'.md5(random_bytes(8).$username);
154+
$nonExistentUsername = '_'.hash('xxh128', random_bytes(8).$username);
155155

156156
// To protect against user enumeration via timing measurements
157157
// we always load both successfully and unsuccessfully

‎src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,6 @@ private function getCacheKey(string $class, array $context): string
142142
return $class.'-'.$context['cache_key'];
143143
}
144144

145-
return $class.md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
145+
return $class.hash('xxh128', serialize($context[AbstractNormalizer::GROUPS] ?? []));
146146
}
147147
}

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ private function getCacheKey(?string $format, array $context): bool|string
730730
unset($context['cache_key']); // avoid artificially different keys
731731

732732
try {
733-
return md5($format.serialize([
733+
return hash('xxh128', $format.serialize([
734734
'context' => $context,
735735
'ignored' => $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES],
736736
]));

‎src/Symfony/Component/VarDumper/Cloner/VarCloner.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Cloner/VarCloner.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function doClone(mixed $var): array
4141
$stub = null; // Stub capturing the main properties of an original item value
4242
// or null if the original value is used directly
4343

44-
$gid = self::$gid ??= md5(random_bytes(6)); // Unique string used to detect the special $GLOBALS variable
44+
$gid = self::$gid ??= hash('xxh128', random_bytes(6)); // Unique string used to detect the special $GLOBALS variable
4545
$arrayStub = new Stub();
4646
$arrayStub->type = Stub::TYPE_ARRAY;
4747
$fromObjCast = false;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.