Exemple #1
0
 /**
  * Encrypts the given data with the given password.
  *
  * @param string $data The data to encrypt.
  * @param string $password The password to encrypt data with.
  * @return array Returns an array containing the encrypted data and some information like the IV if used.
  *                  0 => [string] encrypted data
  *                  1 => [string] password as hex string
  *                  2 => [int] options used (the bitwise disjunction value)
  *                  3 => [string] iv used for encryption
  *                  4 => [int] cipher mode used
  * @see http://php.net/manual/en/function.openssl-encrypt.php openssl_encrypt function reference
  * @throws OpenSSLException when the cipher cannot encrypt the data.
  * @since 0.3.5
  */
 public function encrypt(string $data, string $password) : array
 {
     OpenSSL::resetErrors();
     if (($encrypted = @openssl_encrypt($data, $this->getCipherDescription(), $password, $this->getOptions(), $this->getIV())) === false) {
         // @codeCoverageIgnoreStart
         throw new OpenSSLException(OpenSSL::getErrors(), 'The given data could not be encrypted.');
         // @codeCoverageIgnoreEnd
     }
     return [$encrypted, StringEncoder::rawToHex($password), $this->getOptions(), $this->getIV(), $this->getMode()];
 }
Exemple #2
0
 /**
  * Makes a hexadecimal token using the configured length.
  * Important: The alphabet is not used with this method. Output chars are [0-1][a-f].
  *            The length is the length of the resulting hex string (it is not the length in bytes, though it may match).
  *
  * @see http://php.net/manual/en/function.random-bytes.php Generates cryptographically secure pseudo-random bytes
  * @return string Returns the generated hex token in lowercase.
  * @since 0.1
  */
 public function makeHex() : string
 {
     $bytes = random_bytes((int) round($this->length / 2, 0, PHP_ROUND_HALF_UP));
     $token = StringEncoder::rawToHex($bytes);
     return substr($token, 0, $this->length);
 }