<commit_before><?php

/**
 * This class provides the functionality to encrypt
 * and decrypt access tokens stored by the application
 * @author <NAME> <<EMAIL>>
 * @link https://github.com/benthedesigner/dropbox
 * @package Dropbox\Oauth
 * @subpackage Storage
 */
namespace Dropbox\OAuth\Storage;

class Encrypter
{	
	// Encryption settings
	const CIPHER = MCRYPT_RIJNDAEL_128;
	const MODE = MCRYPT_MODE_CBC;
	const KEY_SIZE = 32;
	const IV_SIZE = 16;
	const IV_SOURCE = MCRYPT_DEV_RANDOM;
	
	/**
	 * Encryption key
	 * @var null|string
	 */
	private $key = null;
	
	/**
	 * Check Mcrypt is loaded and set the encryption key
	 * @param string $key
	 * @return void
	 */
	public function __construct($key)
	{
		if(!extension_loaded('mcrypt')){
			throw new \Dropbox\Exception('The storage encrypter requires the MCrypt extension');
		} elseif(($length = mb_strlen($key, '8bit')) !== self::KEY_SIZE){
			throw new \Dropbox\Exception('Expecting a ' .  self::KEY_SIZE . ' byte key, got ' . $length);
		} else {
			// Set the encryption key
			$this->key = $key;
		}
	}
	
	/**
	 * Encrypt the OAuth token
	 * @param \stdClass $token
	 * @return string
	 */
	public function encrypt($token)
	{
		$data = serialize($token);
		$iv = mcrypt_create_iv(self::IV_SIZE, self::IV_SOURCE);
		$cipherText = mcrypt_encrypt(self::CIPHER, $this->key, $data, self::MODE, $iv);
		return base64_encode($iv . $cipherText);
	}
	
	/**
	 * Decrypt the ciphertext
	 * @param string $cipherText
	 * @return object \stdClass
	 */
	public function decrypt($cipherText)
	{
		$cipherText = base64_decode($cipherText);
		$iv = substr($cipherText, 0, self::IV_SIZE);
		$cipherText = substr($cipherText, self::IV_SIZE);
		$data = mcrypt_decrypt(self::CIPHER, $this->key, $cipherText, self::MODE, $iv);
		return unserialize($data);
	}
}
<commit_msg>Switched MCRYPT_DEV_RANDOM for MCRYPT_DEV_URANDOM to fix slow encryption/decryption<commit_after><?php

/**
 * This class provides the functionality to encrypt
 * and decrypt access tokens stored by the application
 * @author <NAME> <<EMAIL>>
 * @link https://github.com/benthedesigner/dropbox
 * @package Dropbox\Oauth
 * @subpackage Storage
 */
namespace Dropbox\OAuth\Storage;

class Encrypter
{	
	// Encryption settings
	const CIPHER = MCRYPT_RIJNDAEL_128;
	const MODE = MCRYPT_MODE_CBC;
	const KEY_SIZE = 32;
	const IV_SIZE = 16;
	const IV_SOURCE = MCRYPT_DEV_URANDOM;
	
	/**
	 * Encryption key
	 * @var null|string
	 */
	private $key = null;
	
	/**
	 * Check Mcrypt is loaded and set the encryption key
	 * @param string $key
	 * @return void
	 */
	public function __construct($key)
	{
		if(!extension_loaded('mcrypt')){
			throw new \Dropbox\Exception('The storage encrypter requires the MCrypt extension');
		} elseif(($length = mb_strlen($key, '8bit')) !== self::KEY_SIZE){
			throw new \Dropbox\Exception('Expecting a ' .  self::KEY_SIZE . ' byte key, got ' . $length);
		} else {
			// Set the encryption key
			$this->key = $key;
		}
	}
	
	/**
	 * Encrypt the OAuth token
	 * @param \stdClass $token
	 * @return string
	 */
	public function encrypt($token)
	{
		$data = serialize($token);
		$iv = mcrypt_create_iv(self::IV_SIZE, self::IV_SOURCE);
		$cipherText = mcrypt_encrypt(self::CIPHER, $this->key, $data, self::MODE, $iv);
		return base64_encode($iv . $cipherText);
	}
	
	/**
	 * Decrypt the ciphertext
	 * @param string $cipherText
	 * @return object \stdClass
	 */
	public function decrypt($cipherText)
	{
		$cipherText = base64_decode($cipherText);
		$iv = substr($cipherText, 0, self::IV_SIZE);
		$cipherText = substr($cipherText, self::IV_SIZE);
		$data = mcrypt_decrypt(self::CIPHER, $this->key, $cipherText, self::MODE, $iv);
		return unserialize($data);
	}
}

{'fraction_non_alphanumeric': 0.14084887807515545, 'fraction_numerical': 0.007028926736955934, 'mean_word_length': 4.06155950752394, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '1767205', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1255, 'n_words': 317}