Skip to content

Commit e321ea5

Browse files
committed
Implement info method on Hasher
`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
1 parent 57437ab commit e321ea5

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

src/Hasher.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,33 @@ class Hasher implements HasherContract
1212
protected $options = [];
1313

1414
/**
15-
* @var array
15+
* @var string[]
1616
*/
1717
protected $supportedAlgorithms = [];
1818

1919
/**
2020
* Constructor.
2121
*
22-
* @param array $supportedAlgorithms
23-
* @param array $options
22+
* @param string[] $supportedAlgorithms
23+
* @param array $options
2424
*/
2525
public function __construct(array $supportedAlgorithms, array $options)
2626
{
2727
$this->supportedAlgorithms = $supportedAlgorithms;
2828
$this->options = $options;
2929
}
3030

31+
/**
32+
* Get information about the given hashed value.
33+
*
34+
* @param string $integrity
35+
* @return array
36+
*/
37+
public function info($integrity)
38+
{
39+
return $this->extractAlgorithms($integrity, $this->options);
40+
}
41+
3142
/**
3243
* Hash the given file.
3344
*
@@ -72,12 +83,8 @@ public function needsRehash($integrity, array $options = [])
7283
{
7384
$options = $this->getOptions($options);
7485

75-
$algorithms = collect(explode($options['delimiter'], $integrity))
76-
->map(function ($hash) {
77-
return head(explode('-', $hash));
78-
});
79-
80-
return $this->getAlgorithms($options) != $algorithms->all();
86+
return $this->extractAlgorithms($integrity, $options)
87+
!= $this->getAlgorithms($options);
8188
}
8289

8390
/**
@@ -94,8 +101,8 @@ protected function getOptions(array $options)
94101
/**
95102
* Get the hashing algorithms from the options.
96103
*
97-
* @param array $options
98-
* @return array
104+
* @param array $options
105+
* @return string[]
99106
* @throws \InvalidArgumentException
100107
*/
101108
protected function getAlgorithms(array $options)
@@ -115,4 +122,18 @@ protected function getAlgorithms(array $options)
115122

116123
return $options['algorithms'];
117124
}
125+
126+
/**
127+
* Extract the algorithms from the integrity.
128+
*
129+
* @param string $integrity
130+
* @param array $options
131+
* @return string[]
132+
*/
133+
protected function extractAlgorithms($integrity, array $options)
134+
{
135+
return array_map(function ($hash) {
136+
return head(explode('-', $hash));
137+
}, explode($options['delimiter'], $integrity));
138+
}
118139
}

tests/HasherTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66

77
class HasherTest extends TestCase
88
{
9+
/**
10+
* @test
11+
*/
12+
public function it_gets_information_about_a_given_integrity()
13+
{
14+
// arrange
15+
16+
$this->app['config']->set('sri.delimiter', '_');
17+
18+
$hasher = $this->app->make(Hasher::class);
19+
20+
// act
21+
22+
$info = $hasher->info('sha256-foo_sha384-bar');
23+
24+
// assert
25+
26+
$this->assertEquals(['sha256', 'sha384'], $info);
27+
}
28+
929
/**
1030
* @test
1131
*/

0 commit comments

Comments
 (0)