Navigation Menu

Skip to content

Commit

Permalink
Replace sha1 and md5 hashing with sha256 algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon authored and fabpot committed Aug 31, 2013
1 parent 98b14f1 commit dd8014a
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 25 deletions.
Expand Up @@ -368,7 +368,7 @@ protected function loadObjectManagerCacheDriver(array $objectManager, ContainerB

if (!isset($cacheDriver['namespace'])) {
// generate a unique namespace for the given application
$cacheDriver['namespace'] = 'sf2'.$this->getMappingResourceExtension().'_'.$objectManager['name'].'_'.md5($container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment'));
$cacheDriver['namespace'] = 'sf2'.$this->getMappingResourceExtension().'_'.$objectManager['name'].'_'.hash('sha256',($container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment')));
}

$cacheDef->addMethodCall('setNamespace', array($cacheDriver['namespace']));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Expand Up @@ -109,7 +109,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
? spl_object_hash($options['group_by'])
: $options['group_by'];

$hash = md5(json_encode(array(
$hash = hash('sha256', json_encode(array(
spl_object_hash($options['em']),
$options['class'],
$propertyHash,
Expand Down
Expand Up @@ -624,7 +624,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
->replaceArgument(1, new Reference('validator.mapping.cache.'.$config['cache']));
$container->setParameter(
'validator.mapping.cache.prefix',
'validator_'.md5($container->getParameter('kernel.root_dir'))
'validator_'.hash('sha256', $container->getParameter('kernel.root_dir'))
);
}
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
$classes = array_diff($classes, $declared);

// the cache is different depending on which classes are already declared
$name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5);
$name = $name.'-'.substr(hash('sha256', implode('|', $classes)), 0, 5);
}

$classes = array_unique($classes);
Expand Down
Expand Up @@ -233,7 +233,7 @@ private function processAnonymousServices(SimpleXMLElement $xml, $file)
if (false !== $nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]')) {
foreach ($nodes as $node) {
// give it a unique name
$id = sprintf('%s_%d', md5($file), ++$count);
$id = sprintf('%s_%d', hash('sha256', $file), ++$count);
$node['id'] = $id;

$definitions[$id] = array($node->service, $file, false);
Expand All @@ -245,7 +245,7 @@ private function processAnonymousServices(SimpleXMLElement $xml, $file)
if (false !== $nodes = $xml->xpath('//container:services/container:service[not(@id)]')) {
foreach ($nodes as $node) {
// give it a unique name
$id = sprintf('%s_%d', md5($file), ++$count);
$id = sprintf('%s_%d', hash('sha256', $file), ++$count);
$node['id'] = $id;

$definitions[$id] = array($node, $file, true);
Expand Down
Expand Up @@ -164,7 +164,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$choices = null !== $options['choices'] ? $options['choices'] : array();

// Reuse existing choice lists in order to increase performance
$hash = md5(json_encode(array($choices, $options['preferred_choices'])));
$hash = hash('sha256', json_encode(array($choices, $options['preferred_choices'])));

if (!isset($choiceListCache[$hash])) {
$choiceListCache[$hash] = new SimpleChoiceList($choices, $options['preferred_choices']);
Expand Down
Expand Up @@ -249,7 +249,7 @@ public function getMetadataBag()
*/
protected function generateId()
{
return sha1(uniqid(mt_rand()));
return hash('sha256', uniqid(mt_rand()));
}

protected function loadSession()
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/HttpCache/Store.php
Expand Up @@ -214,7 +214,7 @@ public function write(Request $request, Response $response)
*/
protected function generateContentDigest(Response $response)
{
return 'en'.sha1($response->getContent());
return 'en'.hash('sha256', $response->getContent());
}

/**
Expand Down Expand Up @@ -377,7 +377,7 @@ private function getCacheKey(Request $request)
return $this->keyCache[$request];
}

return $this->keyCache[$request] = 'md'.sha1($request->getUri());
return $this->keyCache[$request] = 'md'.hash('sha256', $request->getUri());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Expand Up @@ -204,7 +204,7 @@ public function collect(Request $request, Response $response, \Exception $except
return;
}

$profile = new Profile(substr(sha1(uniqid(mt_rand(), true)), 0, 6));
$profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
$profile->setTime(time());
$profile->setUrl($request->getUri());
$profile->setIp($request->getClientIp());
Expand Down
Expand Up @@ -31,7 +31,7 @@ public function testRenderWithControllerAndSigner()
{
$strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));

$this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=5RZ1IkwF487EaXt6buHka73CCtQ%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
$this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=2RweanrYElMFCPCuRjoIUqaG2vpMpjtGqvqj9pUFLxA%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
}

public function testRenderWithUri()
Expand Down
Expand Up @@ -623,7 +623,7 @@ public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
$r = new \ReflectionObject($this->store);
$m = $r->getMethod('save');
$m->setAccessible(true);
$m->invoke($this->store, 'md'.sha1('http://localhost/'), serialize($tmp));
$m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));

// build subsequent request; should be found but miss due to freshness
$this->request('GET', '/');
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php
Expand Up @@ -89,7 +89,7 @@ public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
$entries = $this->getStoreMetadata($cacheKey);
list ($req, $res) = $entries[0];

$this->assertEquals('ena94a8fe5ccb19ba61c4c0873d391e987982fbbd3', $res['x-content-digest'][0]);
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
}

public function testFindsAStoredEntryWithLookup()
Expand Down Expand Up @@ -139,7 +139,7 @@ public function testRestoresResponseContentFromEntityStoreWithLookup()
{
$this->storeSimpleEntry();
$response = $this->store->lookup($this->request);
$this->assertEquals($this->getStorePath('en'.sha1('test')), $response->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->getContent());
}

public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
Expand Down Expand Up @@ -182,9 +182,9 @@ public function testStoresMultipleResponsesForEachVaryCombination()
$res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
$this->store->write($req3, $res3);

$this->assertEquals($this->getStorePath('en'.sha1('test 3')), $this->store->lookup($req3)->getContent());
$this->assertEquals($this->getStorePath('en'.sha1('test 2')), $this->store->lookup($req2)->getContent());
$this->assertEquals($this->getStorePath('en'.sha1('test 1')), $this->store->lookup($req1)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());

$this->assertCount(3, $this->getStoreMetadata($key));
}
Expand All @@ -194,17 +194,17 @@ public function testOverwritesNonVaryingResponseWithStore()
$req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
$key = $this->store->write($req1, $res1);
$this->assertEquals($this->getStorePath('en'.sha1('test 1')), $this->store->lookup($req1)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());

$req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
$res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
$this->store->write($req2, $res2);
$this->assertEquals($this->getStorePath('en'.sha1('test 2')), $this->store->lookup($req2)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());

$req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
$key = $this->store->write($req3, $res3);
$this->assertEquals($this->getStorePath('en'.sha1('test 3')), $this->store->lookup($req3)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());

$this->assertCount(2, $this->getStoreMetadata($key));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/UriSigner.php
Expand Up @@ -67,6 +67,6 @@ public function check($uri)

private function computeHash($uri)
{
return urlencode(base64_encode(hash_hmac('sha1', $uri, $this->secret, true)));
return urlencode(base64_encode(hash_hmac('sha256', $uri, $this->secret, true)));
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Templating/Asset/UrlPackage.php
Expand Up @@ -73,7 +73,7 @@ public function getBaseUrl($path)
return $this->baseUrls[0];

default:
return $this->baseUrls[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
return $this->baseUrls[fmod(hexdec(substr(hash('sha256', $path), 0, 10)), $count)];
}
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Templating/Loader/CacheLoader.php
Expand Up @@ -50,7 +50,7 @@ public function __construct(LoaderInterface $loader, $dir)
*/
public function load(TemplateReferenceInterface $template)
{
$key = md5($template->getLogicalName());
$key = hash('sha256', $template->getLogicalName());
$dir = $this->dir.DIRECTORY_SEPARATOR.substr($key, 0, 2);
$file = substr($key, 2).'.tpl';
$path = $dir.DIRECTORY_SEPARATOR.$file;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Templating/PhpEngine.php
Expand Up @@ -86,7 +86,7 @@ public function __construct(TemplateNameParserInterface $parser, LoaderInterface
public function render($name, array $parameters = array())
{
$storage = $this->load($name);
$key = md5(serialize($storage));
$key = hash('sha256', serialize($storage));
$this->current = $key;
$this->parents[$key] = null;

Expand Down

0 comments on commit dd8014a

Please sign in to comment.