vendor/sulu/form-bundle/Dynamic/Checksum.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\FormBundle\Dynamic;
  11. use Symfony\Component\PasswordHasher\Hasher\MessageDigestPasswordHasher;
  12. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  13. /**
  14.  * Checksum.
  15.  */
  16. class Checksum
  17. {
  18.     /**
  19.      * @var string
  20.      */
  21.     private $secret;
  22.     /**
  23.      * @var MessageDigestPasswordEncoder|MessageDigestPasswordHasher
  24.      */
  25.     private $encoder;
  26.     public function __construct(string $secret)
  27.     {
  28.         $this->secret $secret;
  29.         $this->encoder \class_exists(MessageDigestPasswordEncoder::class)
  30.             ? new MessageDigestPasswordEncoder()
  31.             : new MessageDigestPasswordHasher();
  32.     }
  33.     /**
  34.      * Check checksum with given parameters.
  35.      */
  36.     public function check(string $checksumstring $typestring $typeIdint $formIdstring $formName): bool
  37.     {
  38.         $checksumRaw $this->createKey($type$typeId$formId$formName);
  39.         if (\class_exists(MessageDigestPasswordEncoder::class)) {
  40.             return $this->encoder->isPasswordValid($checksum$checksumRaw$this->secret);
  41.         }
  42.         return $this->encoder->verify($checksum$checksumRaw$this->secret);
  43.     }
  44.     /**
  45.      * Create a key with given parameteres.
  46.      */
  47.     private function createKey(string $typestring $typeIdint $formIdstring $formName): string
  48.     {
  49.         return $type $typeId $formId $formName;
  50.     }
  51.     /**
  52.      * Create a checksum and encode with secret and given parameters.
  53.      */
  54.     public function get(string $typestring $typeIdint $formIdstring $formName): string
  55.     {
  56.         $checksumRaw $this->createKey($type$typeId$formId$formName);
  57.         if (\class_exists(MessageDigestPasswordEncoder::class)) {
  58.             return $this->encoder->encodePassword($checksumRaw$this->secret);
  59.         }
  60.         return $this->encoder->hash($checksumRaw$this->secret);
  61.     }
  62. }