Skip to content

Commit

Permalink
New: Add Hmac helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Feb 4, 2025
1 parent 3e97663 commit 34e93f9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
33 changes: 30 additions & 3 deletions Classes/EelHelper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Security\Cryptography\HashService;
use Neos\Flow\Validation\Validator\EmailAddressValidator;
use function implode;
use function is_array;
Expand All @@ -23,11 +24,13 @@
use function trim;
use function ucwords;

/**
* @Flow\Proxy(false)
*/
class StringHelper implements ProtectedContextAwareInterface
{
/**
* @Flow\Inject
* @var HashService
*/
protected $hashService;

/**
* Generates a BEM string
Expand All @@ -42,6 +45,30 @@ public function BEM($block = null, $element = null, $modifiers = []): ?string
return BEMService::getClassNamesString($block, $element, $modifiers);
}

/**
* Generate a hash (HMAC) for a given string
*
* @param string $string The string for which a hash should be generated
* @return string The hash of the string
* @throws InvalidArgumentForHashGenerationException if something else than a string was given as parameter
*/
public function generateHmac($string): string
{
return $this->hashService->generateHmac((string) $string);
}

/**
* Tests if a string $string matches the HMAC given by $hash.
*
* @param string $string The string which should be validated
* @param string $hmac The hash of the string
* @return boolean true if string and hash fit together, false otherwise.
*/
public function validateHmac($string, $hmac)
{
return ($this->generateHmac($string) === $hmac);
}

/**
* Minify JavaScript
*
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,23 @@ Examples:

Returns the string with one occurrence replaced

### `Carbon.String.generateHmac(string)`

Generate a hash (HMAC) for a given string

- `string` (string, required) The string for which a hash should be generated

**Return** The hash of the string

### `Carbon.String.validateHmac(string, hmac)`

Tests if a string $string matches the HMAC given by $hash.

- `string` (string, required) The string which should be validated
- `hmac` (string, required) The hash of the string

**Return** true if string and hash fit together, false otherwise.

## Number Helper

### `Carbon.Number.format(number, decimals, dec_point, thousands_sep)`
Expand Down

0 comments on commit 34e93f9

Please sign in to comment.