Encrypts a string with a password, using a slow key derivation function
to make password cracking more expensive.
/** * @param $username * @param $passwordHash * @return string */ public static function generateToken($username, $passwordHash) { $data = time() - 1 . '|' . $username; $token = Crypto::encryptWithPassword($data, $passwordHash); return $token; }
/** * @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException */ public function testDecryptHexAsRaw() { $ciphertext = Crypto::encryptWithPassword('testdata', 'password', false); Crypto::decryptWithPassword($ciphertext, 'password', true); }
$date_errors = DateTime::getLastErrors(); if ($date_errors['warning_count'] + $date_errors['error_count'] > 0) { $errors = true; response(VALIDATION_DATE_INVALID, $errors); } if (strtotime($expiration_date) > strtotime("today +30 days")) { $errors = true; response(VALIDATION_DATE_INVALID, $errors); } // If all of the above validation checks pass, continue on if (!$errors) { // Create an array of data to be encrypted $data = serialize(array("message" => $message, "email_sender" => $email_sender)); // Encrypt data, reference: https://github.com/defuse/php-encryption/ try { $data_encrypted = Crypto::encryptWithPassword($data, $password); } catch (Ex\EnvironmentIsBrokenException $ex) { response(ENCRYPTION_UNSAFE, true); } // Store the encrypted data $array = array('secret' => bin2hex($data_encrypted), 'expiration_date' => strtotime($expiration_date . ' +1 day')); $item = $collection->item(); if ($item->post($array)) { $item->event('log')->post(['action' => 'created']); $id = $item->getKey(); } else { response($item->getStatus(), true); } // Send email to recipient if (!empty($email_recipient)) { // Email body
/** * @inheritDoc */ public function encodeToken($value) { return Crypto::encryptWithPassword($value, $this->key, true); }