Skip to content

Instantly share code, notes, and snippets.

@albe
Created December 3, 2014 16:25
Show Gist options
  • Save albe/71abc5e98b6700a26f15 to your computer and use it in GitHub Desktop.
Save albe/71abc5e98b6700a26f15 to your computer and use it in GitHub Desktop.
<?php
namespace Albe\Extensions\Service;
/* *
* This script belongs to the TYPO3 Flow package "Trackmyrace.Portal". *
* *
* */
use TYPO3\Flow\Annotations as Flow;
/**
* The Encryption Service provides methods to safely encrypt and decrypt data symmetrically via AES-256
*
* @Flow\Scope("singleton")
*/
class SymmetricEncryption {
const METHOD = MCRYPT_RIJNDAEL_128;
const MODE = MCRYPT_MODE_CBC;
/**
* @var integer
*/
protected $ivSize;
/**
* @var string
*/
protected $encryptionKey;
public function initializeObject() {
if (!extension_loaded('mcrypt')) {
throw new \TYPO3\Flow\Exception('The PHP extension "mcrypt" must be installed and loaded in order to use the Encryption Service.', 1368467775);
}
$this->ivSize = mcrypt_get_iv_size(self::METHOD, self::MODE);
}
/**
* @return string The configured encryption key stored in Data/Persistent/EncryptionKey
* @throws \TYPO3\Flow\Security\Exception\MissingConfigurationException
*/
protected function getEncryptionKey() {
if ($this->encryptionKey === NULL) {
if (!file_exists(FLOW_PATH_DATA . 'Persistent/SymmetricEncryptionKey')) {
file_put_contents(FLOW_PATH_DATA . 'Persistent/SymmetricEncryptionKey', \TYPO3\Flow\Utility\Algorithms::generateRandomBytes(mcrypt_get_key_size(self::METHOD, self::MODE)));
}
$this->encryptionKey = file_get_contents(FLOW_PATH_DATA . 'Persistent/SymmetricEncryptionKey');
if ($this->encryptionKey === FALSE || $this->encryptionKey === '') {
throw new \TYPO3\Flow\Security\Exception\MissingConfigurationException('No encryption key for the HashService was found and none could be created at "' . FLOW_PATH_DATA . 'Persistent/AesEncryptionKey"', 1258991855);
}
}
return $this->encryptionKey;
}
/**
* Encrypt data with the private encryption key using AES-256
*
* @param string $value The message to encrypt
* @param string $encryptionKey Optional. The encryption key to use
* @return string
*/
public function encrypt($value, $encryptionKey = NULL) {
$iv = mcrypt_create_iv($this->ivSize, MCRYPT_RAND);
if ($encryptionKey === NULL) {
$encryptionKey = $this->getEncryptionKey();
}
return $iv . mcrypt_encrypt(self::METHOD, $encryptionKey, $value, self::MODE, $iv);
}
/**
* Decrypt data with the private encryption key using AES-256
*
* @param string $value The encrypted message to decipher
* @param string $encryptionKey Optional. The encryption key to use
* @return string
*/
public function decrypt($value, $encryptionKey = NULL) {
$iv = substr($value, 0, $this->ivSize);
if ($encryptionKey === NULL) {
$encryptionKey = $this->getEncryptionKey();
}
return trim(mcrypt_decrypt(self::METHOD, $encryptionKey, substr($value, $this->ivSize), self::MODE, $iv), "\0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment