Skip to content

Commit

Permalink
Renamed sha2 hash algorithm names for clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegroskurth committed Jul 17, 2018
1 parent 4f6bd42 commit be5be45
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Config/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Configuration implements ArraySerializableInterface
*
* @var string[]
*/
protected $fileChecksums = ['sha256', 'sha1', 'md5'];
protected $fileChecksums = ['sha2-256', 'sha1', 'md5'];

/**
* Array of vault configurations.
Expand Down
8 changes: 4 additions & 4 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Storeman\Hash\Algorithm\HashAlgorithmInterface;
use Storeman\Hash\Algorithm\Md5;
use Storeman\Hash\Algorithm\Sha1;
use Storeman\Hash\Algorithm\Sha256;
use Storeman\Hash\Algorithm\Sha512;
use Storeman\Hash\Algorithm\Sha2_256;
use Storeman\Hash\Algorithm\Sha2_512;
use Storeman\Hash\HashProvider;
use Storeman\IndexBuilder\IndexBuilderInterface;
use Storeman\IndexBuilder\StandardIndexBuilder;
Expand Down Expand Up @@ -80,8 +80,8 @@ public function __construct()
$this->addHashAlgorithm('crc32', Crc32::class);
$this->addHashAlgorithm('md5', Md5::class);
$this->addHashAlgorithm('sha1', Sha1::class);
$this->addHashAlgorithm('sha256', Sha256::class);
$this->addHashAlgorithm('sha512', Sha512::class);
$this->addHashAlgorithm('sha2-256', Sha2_256::class);
$this->addHashAlgorithm('sha2-512', Sha2_512::class);

$this->delegate->add('hashFunctions', function(Container $container) {

Expand Down
12 changes: 11 additions & 1 deletion src/Hash/Algorithm/AbstractPhpHashAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class AbstractPhpHashAlgorithm implements HashAlgorithmInterface
*/
public function initialize(): void
{
$this->context = hash_init($this->getName());
$this->context = hash_init($this->getPhpHashAlgorithmName());
}

/**
Expand All @@ -33,4 +33,14 @@ public function finalize(): string

return hash_final($this->context);
}

/**
* Allows to override the hash algo name.
*
* @return string
*/
protected function getPhpHashAlgorithmName(): string
{
return $this->getName();
}
}
14 changes: 0 additions & 14 deletions src/Hash/Algorithm/Sha256.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Hash/Algorithm/Sha2_256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Storeman\Hash\Algorithm;

final class Sha2_256 extends AbstractPhpHashAlgorithm
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'sha2-256';
}

/**
* {@inheritdoc}
*/
protected function getPhpHashAlgorithmName(): string
{
return 'sha256';
}
}
22 changes: 22 additions & 0 deletions src/Hash/Algorithm/Sha2_512.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Storeman\Hash\Algorithm;

final class Sha2_512 extends AbstractPhpHashAlgorithm
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'sha2-512';
}

/**
* {@inheritdoc}
*/
protected function getPhpHashAlgorithmName(): string
{
return 'sha512';
}
}
14 changes: 0 additions & 14 deletions src/Hash/Algorithm/Sha512.php

This file was deleted.

8 changes: 4 additions & 4 deletions tests/FileReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Storeman\FileReader;
use Storeman\Hash\Algorithm\Md5;
use Storeman\Hash\Algorithm\Sha1;
use Storeman\Hash\Algorithm\Sha256;
use Storeman\Hash\Algorithm\Sha2_256;
use Storeman\Hash\HashContainer;

class FileReaderTest extends TestCase
Expand All @@ -20,12 +20,12 @@ public function test()

$configuration = $this->getConfigurationMock([
'getPath' => $testVault->getBasePath(),
'getFileChecksums' => ['sha256', 'sha1', 'md5'],
'getFileChecksums' => ['sha2-256', 'sha1', 'md5'],
]);

$fileReader = new FileReader($configuration, [
'sha1' => new Sha1(),
'sha256' => new Sha256(),
'sha2-256' => new Sha2_256(),
'md5' => new Md5(),
]);

Expand All @@ -38,7 +38,7 @@ public function test()
$this->assertTrue(is_resource($stream));
$this->assertEquals('Hello World', stream_get_contents($stream));
$this->assertEquals('0a4d55a8d778e5022fab701977c5d840bbc486d0', $hashContainer->getHash('sha1'));
$this->assertEquals('a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e', $hashContainer->getHash('sha256'));
$this->assertEquals('a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e', $hashContainer->getHash('sha2-256'));
$this->assertEquals('b10a8db164e0754105b7a99be72e3fe5', $hashContainer->getHash('md5'));

fclose($stream);
Expand Down
6 changes: 3 additions & 3 deletions tests/Hash/AggregateHashAlgorithmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Storeman\Hash\AggregateHashAlgorithm;
use Storeman\Hash\Algorithm\Md5;
use Storeman\Hash\Algorithm\Sha1;
use Storeman\Hash\Algorithm\Sha256;
use Storeman\Hash\Algorithm\Sha2_256;

class AggregateHashAlgorithmTest extends TestCase
{
Expand All @@ -16,7 +16,7 @@ public function test()
'md5' => new Md5(),
'sha1' => new Sha1(),
]);
$hash->addAlgorithm(new Sha256());
$hash->addAlgorithm(new Sha2_256());

$hash->initialize();

Expand All @@ -30,7 +30,7 @@ public function test()
$this->assertEquals([
'md5' => 'b10a8db164e0754105b7a99be72e3fe5',
'sha1' => '0a4d55a8d778e5022fab701977c5d840bbc486d0',
'sha256' => 'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e',
'sha2-256' => 'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e',
], $hashes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Storeman\Test\Hash\Algorithm;

use Storeman\Hash\Algorithm\HashAlgorithmInterface;
use Storeman\Hash\Algorithm\Sha256;
use Storeman\Hash\Algorithm\Sha2_256;

class Sha256Test extends AbstractAlgorithmTest
class Sha2_256Test extends AbstractAlgorithmTest
{
protected function getTestCases(): array
{
Expand All @@ -16,6 +16,6 @@ protected function getTestCases(): array

protected function getFunction(): HashAlgorithmInterface
{
return new Sha256();
return new Sha2_256();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Storeman\Test\Hash\Algorithm;

use Storeman\Hash\Algorithm\HashAlgorithmInterface;
use Storeman\Hash\Algorithm\Sha512;
use Storeman\Hash\Algorithm\Sha2_512;

class Sha512Test extends AbstractAlgorithmTest
class Sha2_512Test extends AbstractAlgorithmTest
{
protected function getTestCases(): array
{
Expand All @@ -16,6 +16,6 @@ protected function getTestCases(): array

protected function getFunction(): HashAlgorithmInterface
{
return new Sha512();
return new Sha2_512();
}
}
18 changes: 9 additions & 9 deletions tests/IndexMerger/StandardIndexMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Storeman\Hash\Algorithm\Crc32;
use Storeman\Hash\Algorithm\Md5;
use Storeman\Hash\Algorithm\Sha1;
use Storeman\Hash\Algorithm\Sha256;
use Storeman\Hash\Algorithm\Sha512;
use Storeman\Hash\Algorithm\Sha2_256;
use Storeman\Hash\Algorithm\Sha2_512;
use Storeman\Hash\HashProvider;
use Storeman\Index\Index;
use Storeman\Index\IndexObject;
Expand Down Expand Up @@ -181,7 +181,7 @@ public function testContentModificationDetectionWithManipulatedMtime()
$indexA = $testVault->getIndex();
$objectA = $indexA->getObjectByPath('file.ext');
$objectA->setBlobId('xxx');
$objectA->getHashes()->addHash('sha256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae');
$objectA->getHashes()->addHash('sha2-256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae');

// obvious change as we can compare file size
$testVault->fwrite('file.ext', 'obvious');
Expand Down Expand Up @@ -256,28 +256,28 @@ public function testIndependentContentAndMetadataModificationHandling()
$localTestVault->touch('file.ext', 1000);

$lastLocalIndex = $localTestVault->getIndex();
$lastLocalIndex->getObjectByPath('file.ext')->getHashes()->addHash('sha256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae');
$lastLocalIndex->getObjectByPath('file.ext')->getHashes()->addHash('sha2-256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae');

$localTestVault->chmod('file.ext', 0777);

$localIndex = $localTestVault->getIndex();
$localIndex->getObjectByPath('file.ext')->getHashes()->addHash('sha256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae');
$localIndex->getObjectByPath('file.ext')->getHashes()->addHash('sha2-256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae');

$remoteTestVault = new TestVault();
$remoteTestVault->fwrite('file.ext', 'bar');
$remoteTestVault->chmod('file.ext', 0644);
$remoteTestVault->touch('file.ext', 1000);

$remoteIndex = $remoteTestVault->getIndex();
$remoteIndex->getObjectByPath('file.ext')->getHashes()->addHash('sha256', 'fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9');
$remoteIndex->getObjectByPath('file.ext')->getHashes()->addHash('sha2-256', 'fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9');

$mergedIndex = $this->getIndexMerger($localTestVault)->merge(new PanickingConflictHandler(), $remoteIndex, $localIndex, $lastLocalIndex);
$indexObject = $mergedIndex->getObjectByPath('file.ext');

$this->assertInstanceOf(IndexObject::class, $indexObject);
$this->assertEquals(0777, $indexObject->getPermissions());
$this->assertEquals(1000, $indexObject->getMtime());
$this->assertEquals('fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9', $indexObject->getHashes()->getHash('sha256'));
$this->assertEquals('fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9', $indexObject->getHashes()->getHash('sha2-256'));
}

protected function getIndexMerger(TestVault $testVault): StandardIndexMerger
Expand All @@ -290,8 +290,8 @@ protected function getIndexMerger(TestVault $testVault): StandardIndexMerger
'crc32' => new Crc32(),
'md5' => new Md5(),
'sha1' => new Sha1(),
'sha256' => new Sha256(),
'sha512' => new Sha512(),
'sha2-256' => new Sha2_256(),
'sha2-512' => new Sha2_512(),
];
$fileReader = new FileReader($configuration, $algorithms);
$hashProvider = new HashProvider($fileReader, $configuration, $algorithms);
Expand Down

0 comments on commit be5be45

Please sign in to comment.