<commit_before><?php
/**
  phpSec - A PHP security library

  @author    <NAME> <<EMAIL>>
  @copyright Copyright (c) <NAME>, 2011
  @link      https://github.com/xqus/phpSec
  @license   http://opensource.org/licenses/mit-license.php The MIT License
  @package   phpSec
 */

/**
 * Provides methods for encrypting data.
 */
class phpsecCrypt {
  const ALGO      = MCRYPT_BLOWFISH;
  const ALGO_MODE = MCRYPT_MODE_CBC;
  const HASH_TYPE = 'sha256';

  /**
   * Encrypt data returning a JSON encoded array safe for storage in a database
   * or file. The array has the following structure before it is encoded:
   * array(
   *   'cdata' => 'Encrypted data, Base 64 encoded',
   *   'iv'    => 'Base64 encoded IV',
   *   'algo'  => 'Algorythm used',
   *   'mode'  => 'Mode used',
   *   'hash'  => 'A SHA256 hash of the data'
   * )
   *
   * @param mixed $data
   *   Data to encrypt.
   *
   * @return string
   *   Serialized array containing the encrypted data along with some meta data.
   */
  public static function encrypt($data, $key) {
    $td = mcrypt_module_open(self::ALGO, '', self::ALGO_MODE, '');

    /* Create IV. */
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);

    /* Get keysize length. */
    $ks = mcrypt_enc_get_key_size($td);

    /* Get key. */
    $key = self::getKey($key, $ks);

    /* Init mcrypt. */
    mcrypt_generic_init($td, $key, $iv);

    /* Prepeare the array with data. */
    $serializedData = serialize($data);

    $encrypted['cdata'] = base64_encode(mcrypt_generic($td, $serializedData));
    $encrypted['hash']  = hash(self::HASH_TYPE, $serializedData);
    $encrypted['algo']  = self::ALGO;<commit_msg>Use phpsecRand::bytes() instead of mcrypt_create_iv() to create initialization vector for the Mcrypt library. The mcrypt_create_iv() is known to be slow, as staed by comments on http://no.php.net/manual/en/function.mcrypt-create-iv.php<commit_after><?php
/**
  phpSec - A PHP security library

  @author    <NAME> <<EMAIL>>
  @copyright Copyright (c) <NAME>, 2011
  @link      https://github.com/xqus/phpSec
  @license   http://opensource.org/licenses/mit-license.php The MIT License
  @package   phpSec
 */

/**
 * Provides methods for encrypting data.
 */
class phpsecCrypt {
  const ALGO      = MCRYPT_BLOWFISH;
  const ALGO_MODE = MCRYPT_MODE_CBC;
  const HASH_TYPE = 'sha256';

  /**
   * Encrypt data returning a JSON encoded array safe for storage in a database
   * or file. The array has the following structure before it is encoded:
   * array(
   *   'cdata' => 'Encrypted data, Base 64 encoded',
   *   'iv'    => 'Base64 encoded IV',
   *   'algo'  => 'Algorythm used',
   *   'mode'  => 'Mode used',
   *   'hash'  => 'A SHA256 hash of the data'
   * )
   *
   * @param mixed $data
   *   Data to encrypt.
   *
   * @return string
   *   Serialized array containing the encrypted data along with some meta data.
   */
  public static function encrypt($data, $key) {
    $td = mcrypt_module_open(self::ALGO, '', self::ALGO_MODE, '');

    /* Create IV. */
    $iv = phpsecRand::bytes(mcrypt_enc_get_iv_size($td));

    /* Get keysize length. */
    $ks = mcrypt_enc_get_key_size($td);

    /* Get key. */
    $key = self::getKey($key, $ks);

    /* Init mcrypt. */
    mcrypt_generic_init($td, $key, $iv);

    /* Prepeare the array with data. */
    $serializedData = serialize($data);

    $encrypted['cdata'] = base64_encode(mcrypt_generic($td, $serializedData));
    $encrypted['hash']  = hash(self::HASH_TYPE, $serializedData);
    $encrypted['algo']  = self::ALGO;
{'fraction_non_alphanumeric': 0.14313835770528685, 'fraction_numerical': 0.008998875140607425, 'mean_word_length': 3.0744558991981674, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '1037859', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1207, 'n_words': 323}