Skip to content

Commit

Permalink
Implement info method on Hasher
Browse files Browse the repository at this point in the history
`Illuminate\Contracts\Hashing\Hasher` added `info` method on Laravel 5.6.
This commit implements this method by returning an array of algorithms for the given integrity hash.

Fixes #5
  • Loading branch information
sebdesign committed Aug 16, 2018
1 parent 57437ab commit e321ea5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/Hasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,33 @@ class Hasher implements HasherContract
protected $options = [];

/**
* @var array
* @var string[]
*/
protected $supportedAlgorithms = [];

/**
* Constructor.
*
* @param array $supportedAlgorithms
* @param array $options
* @param string[] $supportedAlgorithms
* @param array $options
*/
public function __construct(array $supportedAlgorithms, array $options)
{
$this->supportedAlgorithms = $supportedAlgorithms;
$this->options = $options;
}

/**
* Get information about the given hashed value.
*
* @param string $integrity
* @return array
*/
public function info($integrity)
{
return $this->extractAlgorithms($integrity, $this->options);
}

/**
* Hash the given file.
*
Expand Down Expand Up @@ -72,12 +83,8 @@ public function needsRehash($integrity, array $options = [])
{
$options = $this->getOptions($options);

$algorithms = collect(explode($options['delimiter'], $integrity))
->map(function ($hash) {
return head(explode('-', $hash));
});

return $this->getAlgorithms($options) != $algorithms->all();
return $this->extractAlgorithms($integrity, $options)
!= $this->getAlgorithms($options);
}

/**
Expand All @@ -94,8 +101,8 @@ protected function getOptions(array $options)
/**
* Get the hashing algorithms from the options.
*
* @param array $options
* @return array
* @param array $options
* @return string[]
* @throws \InvalidArgumentException
*/
protected function getAlgorithms(array $options)
Expand All @@ -115,4 +122,18 @@ protected function getAlgorithms(array $options)

return $options['algorithms'];
}

/**
* Extract the algorithms from the integrity.
*
* @param string $integrity
* @param array $options
* @return string[]
*/
protected function extractAlgorithms($integrity, array $options)
{
return array_map(function ($hash) {
return head(explode('-', $hash));
}, explode($options['delimiter'], $integrity));
}
}
20 changes: 20 additions & 0 deletions tests/HasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@

class HasherTest extends TestCase
{
/**
* @test
*/
public function it_gets_information_about_a_given_integrity()
{
// arrange

$this->app['config']->set('sri.delimiter', '_');

$hasher = $this->app->make(Hasher::class);

// act

$info = $hasher->info('sha256-foo_sha384-bar');

// assert

$this->assertEquals(['sha256', 'sha384'], $info);
}

/**
* @test
*/
Expand Down

0 comments on commit e321ea5

Please sign in to comment.