Skip to content

Commit

Permalink
refactor: hash AbstractCrypt for better modularity
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Dec 29, 2024
1 parent 9152706 commit a1c744b
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/Crypt/AbstractCrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ public function hashFromParams(string $storeKey, array $data, string $hashParams
*/
$hashParamsArr = \explode($paramSeparator, $hashParams);

$paramsVal = '';
foreach ($hashParamsArr as $paramKey) {
$paramsVal .= $this->recursiveFind($data, $paramKey);
}

$hashVal = $this->concatenateHashKey($storeKey, $paramsVal);
$hashVal = $this->buildHashString($data, $hashParamsArr, '', $storeKey);

return $this->hashString($hashVal, $storeKey);
}
Expand All @@ -88,6 +83,41 @@ protected function concatenateHashKey(string $hashKey, string $hashString): stri
return $hashString.$hashKey;
}

/**
* @param array<string, mixed> $data data from which the hash string will be built
* @param string[] $paramNames parameter names that will be used in hash calculation
* @param string $separator separator between the parameter values
* @param string|null $storeKey secret key of the API, will be attached to the hash string if provided
*
* @return string string data to be hashed
*/
protected function buildHashString(array $data, array $paramNames, string $separator = '', ?string $storeKey = null): string
{
$paramsVal = \implode($separator, $this->buildHashData($data, $paramNames));

if (null !== $storeKey) {
$paramsVal = $this->concatenateHashKey($storeKey, $paramsVal);
}

return $paramsVal;
}

/**
* @param array<string, mixed> $data data from which the hash string will be built
* @param string[] $paramNames parameter names that will be used in hash calculation
*
* @return string[]
*/
protected function buildHashData(array $data, array $paramNames): array
{
$paramsVal = [];
foreach ($paramNames as $paramKey) {
$paramsVal[] = $this->recursiveFind($data, $paramKey);
}

return $paramsVal;
}

/**
* @param array<string, mixed> $haystack (multidimensional) array
* @param string $needle key name that will be searched in the (multidimensional) array
Expand Down

0 comments on commit a1c744b

Please sign in to comment.