Skip to content

Commit

Permalink
[TASK] Add possibility to create HashValue from actual payload
Browse files Browse the repository at this point in the history
The new factory method `HashValue::hash('test')` creates the
hash from the raw payload `'test'`. This is a simpler shortcut
for using `HashValue::create(hash('sha256', 'test', true))`.

Resolves: #103772
Releases: main, 12.4
Change-Id: Id201e166eeabc856b8d8dc498cc73cde8d5eb801
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/84100
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
  • Loading branch information
ohader authored and georgringer committed May 1, 2024
1 parent 268bfed commit d9eee9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Expand Up @@ -27,6 +27,12 @@ final class HashValue implements \Stringable, SourceValueInterface
{
public readonly string $value;

public static function hash(string $payload, HashType $type = HashType::sha256): self
{
$value = hash($type->value, $payload, true);
return self::create($value, $type);
}

public static function create(string $value, HashType $type = HashType::sha256): self
{
return new self($value, $type);
Expand Down
Expand Up @@ -59,12 +59,18 @@ public function hashProxyIsCompiled(): void
}

#[Test]
public function hashValueIsCompiled(): void
public function hashValueIsCompiledUsingHashFactory(): void
{
$policy = (new Policy())->extend(Directive::ScriptSrc, HashValue::hash('test'));
self::assertSame("script-src 'sha256-n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg='", $policy->compile($this->nonce));
}

#[Test]
public function hashValueIsCompiledUsingCreateFactory(): void
{
$hash = hash('sha256', 'test', true);
$hashB64 = base64_encode($hash);
$policy = (new Policy())->extend(Directive::ScriptSrc, HashValue::create($hash));
self::assertSame("script-src 'sha256-$hashB64'", $policy->compile($this->nonce));
self::assertSame("script-src 'sha256-n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg='", $policy->compile($this->nonce));
}

#[Test]
Expand Down

0 comments on commit d9eee9e

Please sign in to comment.