<commit_before><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 *
 * http://www.warpconduit.net/2013/04/14/highly-secure-data-encryption-decryption-made-easy-with-php-mcrypt-rijndael-256-and-cbc/
 */

namespace Mautic\CoreBundle\Helper;


use Mautic\CoreBundle\Factory\MauticFactory;

class EncryptionHelper
{

    private $key;

    /**
     * @param MauticFactory $factory
     *
     * @throws \RuntimeException if the mcrypt extension is not enabled
     */
    public function __construct(MauticFactory $factory)
    {
        // Toss an Exception back if mcrypt is not found
        if (!extension_loaded('mcrypt')) {
            throw new \RuntimeException($factory->getTranslator()->trans('mautic.core.error.no.mcrypt'));
        }

        $this->key = $factory->getParameter('secret_key');
    }

    /**
     * Returns a 64 character string
     *
     * @return string
     */
    static public function generateKey()
    {
        return hash('sha256', uniqid(mt_rand(), true));
    }

    /**
     * Encrypt string
     *
     * @param $encrypt
     *
     * @return string
     */
    public function encrypt ($encrypt)
    {
        $encrypt   = serialize($encrypt);
        $iv        = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
        $key       = pack('H*', sprintf('%u', CRC32($this->key)));
        $mac       = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32));
        $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt . $mac, MCRYPT_MODE_CBC, $iv);
        $encoded   = base64_encode($passcrypt) . '|' . base64_encode($iv);

        return $encoded;
    }

    /**
     * Decrypt string
     *
     * @param $decrypt
     *
     * @return bool|mixed|string
     */
    public function decrypt ($decrypt)
    {
        $decrypt = explode('|', $decrypt . '|');
        $decoded = base64_decode($decrypt[0]);
        $iv      = base64_decode($decrypt[1]);
        if (strlen($iv) !== mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)) {
            return false;
        }
        $key       = pack('H*', sprintf('%u', CRC32($this->key)));
        $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
        $mac       = substr($decrypted, -64);
        $decrypted = substr($decrypted, 0, -64);
        $calcmac   = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
        if ($calcmac !== $mac) {
            return false;
        }
        $decrypted = unserialize($decrypted);

        return $decrypted;
    }
}
<commit_msg>Reverted bad code that was inadvertently committed that caused and fixes #1391<commit_after><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 *
 * http://www.warpconduit.net/2013/04/14/highly-secure-data-encryption-decryption-made-easy-with-php-mcrypt-rijndael-256-and-cbc/
 */

namespace Mautic\CoreBundle\Helper;


use Mautic\CoreBundle\Factory\MauticFactory;

class EncryptionHelper
{

    private $key;

    /**
     * @param MauticFactory $factory
     *
     * @throws \RuntimeException if the mcrypt extension is not enabled
     */
    public function __construct(MauticFactory $factory)
    {
        // Toss an Exception back if mcrypt is not found
        if (!extension_loaded('mcrypt')) {
            throw new \RuntimeException($factory->getTranslator()->trans('mautic.core.error.no.mcrypt'));
        }

        $this->key = $factory->getParameter('secret_key');
    }

    /**
     * Returns a 64 character string
     *
     * @return string
     */
    static public function generateKey()
    {
        return hash('sha256', uniqid(mt_rand(), true));
    }

    /**
     * Encrypt string
     *
     * @param $encrypt
     *
     * @return string
     */
    public function encrypt ($encrypt)
    {
        $encrypt   = serialize($encrypt);
        $iv        = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
        $key       = pack('H*', $this->key);
        $mac       = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32));
        $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt . $mac, MCRYPT_MODE_CBC, $iv);
        $encoded   = base64_encode($passcrypt) . '|' . base64_encode($iv);

        return $encoded;
    }

    /**
     * Decrypt string
     *
     * @param $decrypt
     *
     * @return bool|mixed|string
     */
    public function decrypt ($decrypt)
    {
        $decrypt = explode('|', $decrypt . '|');
        $decoded = base64_decode($decrypt[0]);
        $iv      = base64_decode($decrypt[1]);
        if (strlen($iv) !== mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)) {
            return false;
        }

        $key       = pack('H*', $this->key);
        $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
        $mac       = substr($decrypted, -64);
        $decrypted = substr($decrypted, 0, -64);
        $calcmac   = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
        if ($calcmac !== $mac) {
            return false;
        }
        $decrypted = unserialize($decrypted);

        return $decrypted;
    }
}

{'fraction_non_alphanumeric': 0.14386281588447652, 'fraction_numerical': 0.023826714801444042, 'mean_word_length': 2.4056545789797172, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5210859', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1853, 'n_words': 337}