7 declare(strict_types=1);
78 private $hashVersionMap = [
79 self::HASH_VERSION_MD5 =>
'md5',
80 self::HASH_VERSION_SHA256 =>
'sha256' 86 private $passwordHashMap = [
87 self::PASSWORD_HASH =>
'',
88 self::PASSWORD_SALT =>
'',
127 $this->random = $random;
130 $this->keys = preg_split(
'/\s+/s', trim((
string)
$deploymentConfig->get(self::PARAM_CRYPT_KEY)));
131 $this->keyVersion = count($this->keys) - 1;
153 if (!in_array(
$version, $types,
true)) {
154 throw new \Exception((
string)
new \
Magento\Framework\
Phrase(
'Not supported cipher version'));
162 public function getHash($password, $salt =
false,
$version = self::HASH_VERSION_LATEST)
164 if ($salt ===
false) {
167 if ($salt ===
true) {
170 if (is_integer($salt)) {
171 $salt = $this->random->getRandomString($salt);
205 $this->explodePasswordHash($hash);
207 foreach ($this->getPasswordVersion() as $hashVersion) {
208 $password = $this->
hash($this->getPasswordSalt() . $password, $hashVersion);
213 $this->getPasswordHash()
222 $this->explodePasswordHash($hash);
223 $hashVersions = $this->getPasswordVersion();
225 return $validateCount
226 ? end($hashVersions) === self::HASH_VERSION_LATEST && count($hashVersions) === 1
236 private function explodePasswordHash($hash)
238 $explodedPassword = explode(self::DELIMITER, $hash, 3);
240 foreach ($this->passwordHashMap as $key => $defaultValue) {
241 $this->passwordHashMap[$key] = (isset($explodedPassword[$key])) ? $explodedPassword[$key] : $defaultValue;
244 return $this->passwordHashMap;
252 private function getPasswordHash()
262 private function getPasswordSalt()
272 private function getPasswordVersion()
274 return array_map(
'intval', explode(self::DELIMITER, $this->passwordHashMap[self::PASSWORD_VERSION]));
287 return $this->keyVersion .
288 ':' . self::CIPHER_AEAD_CHACHA20POLY1305 .
289 ':' . base64_encode($crypt->encrypt(
$data));
300 $crypt = $this->getCrypt();
301 if (
null === $crypt) {
304 return $this->keyVersion .
305 ':' . $this->getCipherVersion() .
306 ':' . base64_encode($crypt->encrypt(
$data));
322 $parts = explode(
':',
$data, 4);
323 $partsCount = count($parts);
327 if (4 === $partsCount) {
333 }
elseif (3 === $partsCount) {
336 $cryptVersion = (int)$cryptVersion;
338 }
elseif (2 === $partsCount) {
339 list($cryptVersion,
$data) = $parts;
341 $cryptVersion = (int)$cryptVersion;
343 }
elseif (1 === $partsCount) {
355 if (
null === $crypt) {
358 return trim($crypt->decrypt(base64_decode((
string)
$data)));
371 if (preg_match(
'/\s/s', $key)) {
372 throw new \Exception((
string)
new \
Magento\Framework\
Phrase(
'The encryption key format is invalid.'));
386 $this->keys[] = $key;
387 $this->keyVersion += 1;
398 return implode(
"\n", $this->keys);
412 private function getCrypt(
414 int $cipherVersion =
null,
417 if (
null === $key &&
null === $cipherVersion) {
418 $cipherVersion = $this->getCipherVersion();
429 if (
null === $cipherVersion) {
434 if ($cipherVersion >= self::CIPHER_AEAD_CHACHA20POLY1305) {
435 return new SodiumChachaIetf($key);
438 if ($cipherVersion === self::CIPHER_RIJNDAEL_128) {
440 $mode = MCRYPT_MODE_ECB;
441 }
elseif ($cipherVersion === self::CIPHER_RIJNDAEL_256) {
443 $mode = MCRYPT_MODE_CBC;
446 $mode = MCRYPT_MODE_ECB;
457 private function getCipherVersion()
459 if (extension_loaded(
'sodium')) {
const CIPHER_RIJNDAEL_128
elseif(isset( $params[ 'redirect_parent']))
isValidHash($password, $hash)
const HASH_VERSION_LATEST
const HASH_VERSION_SHA256
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
encryptWithFastestAvailableAlgorithm($data)
hash($data, $version=self::HASH_VERSION_LATEST)
__construct(Random $random, DeploymentConfig $deploymentConfig)
static compareStrings($expected, $actual)
validateHash($password, $hash)
const DEFAULT_SALT_LENGTH
validateHashVersion($hash, $validateCount=false)
getHash($password, $salt=false, $version=self::HASH_VERSION_LATEST)
const CIPHER_AEAD_CHACHA20POLY1305
const CIPHER_RIJNDAEL_256