<commit_before><?php

/**
 * Openssl.php
 * 
 * PHP version 5
 * 
 * @category Dcrypt
 * @package  Dcrypt
 * @author   <NAME> (mmeyer2k) <<EMAIL>>
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
 * @link     https://github.com/mmeyer2k/dcrypt
 */

namespace Dcrypt;

/**
 * Symmetric OpenSSL wrapper functions.
 * 
 * @category Dcrypt
 * @package  Dcrypt
 * @author   <NAME> (mmeyer2k) <<EMAIL>>
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
 * @link     https://github.com/mmeyer2k/dcrypt
 */
class Openssl extends Cryptobase
{

    const cipher = 'aes-256-cbc';
    const algo = 'sha256';
    const rij = 'rijndael-128';

    /**
     * Decrypt cyphertext
     * 
     * @param string $cyphertext Cypher text to decrypt
     * @param string $key        Key that should be used to decrypt input data
     * 
     * @return string|boolean Returns false on checksum validation failure
     */
    public static function decrypt($cyphertext, $key)
    {
        // Normalize (de/en)cryption key (by-ref)
        self::_init($key, self::rij, 'cbc', self::algo);

        // Determine that size of the IV in bytes
        $ivsize = openssl_cipher_iv_length(self::cipher);

        // Find the IV at the beginning of the cypher text
        $iv = substr($cyphertext, 0, $ivsize);

        // Gather the checksum portion of the cypher text
        $chksum = substr($cyphertext, $ivsize, strlen($key));

        // Gather message portion of cyphertext after iv and checksum
        $message = substr($cyphertext, $ivsize + strlen($key));

        // Calculate verification checksum
        $verify = self::_checksum($message, $iv, $key, 'cbc', self::rij, self::algo);

        // Verify HMAC before decrypting... return false if corrupt.
        if (!Strcmp::equals($verify, $chksum)) {
            return false;
        }

        // Decrypt message and return
        return openssl_decrypt($message, self::cipher, $key, 1, $iv);
    }

    /**
     * Encrypt plaintext
     * 
     * @param string $plaintext Plaintext string to encrypt.
     * @param string $key       Key used to encrypt data.
     * 
     * @return string 
     */
    public static function encrypt($plaintext, $key)
    {
        // Normalize (de/en)cryption key (by-ref)
        self::_init($key, self::rij, 'cbc', self::algo);

        // Generate IV of appropriate size.
        $iv = Random::get(16);

        // Encrypt the plaintext
        $message = openssl_encrypt($plaintext, self::cipher, $key, 1, $iv);

        // Create the cypher text prefix (iv + checksum)
        $prefix = $iv . self::_checksum($message, $iv, $key, 'cbc', self::rij, self::algo);

        // Return prefix + cyphertext
        return $prefix . $message;
    }

}
<commit_msg>Update Openssl.php<commit_after><?php

/**
 * Openssl.php
 * 
 * PHP version 5
 * 
 * @category Dcrypt
 * @package  Dcrypt
 * @author   <NAME> (mmeyer2k) <<EMAIL>>
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
 * @link     https://github.com/mmeyer2k/dcrypt
 */

namespace Dcrypt;

/**
 * Symmetric OpenSSL wrapper functions.
 * 
 * @category Dcrypt
 * @package  Dcrypt
 * @author   <NAME> (mmeyer2k) <<EMAIL>>
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
 * @link     https://github.com/mmeyer2k/dcrypt
 */
class Openssl extends Cryptobase
{

    const cipher = 'aes-256-cbc';
    const algo = 'sha256';
    const rij = 'rijndael-<PASSWORD>';
    const ivsize = 16;

    /**
     * Decrypt cyphertext
     * 
     * @param string $cyphertext Cypher text to decrypt
     * @param string $key        Key that should be used to decrypt input data
     * 
     * @return string|boolean Returns false on checksum validation failure
     */
    public static function decrypt($cyphertext, $key)
    {
        // Normalize (de/en)cryption key (by-ref)
        self::_init($key, self::rij, 'cbc', self::algo);

        // Find the IV at the beginning of the cypher text
        $iv = substr($cyphertext, 0, self::ivsize);

        // Gather the checksum portion of the cypher text
        $chksum = substr($cyphertext, self::ivsize, strlen($key));

        // Gather message portion of cyphertext after iv and checksum
        $message = substr($cyphertext, self::ivsize + strlen($key));

        // Calculate verification checksum
        $verify = self::_checksum($message, $iv, $key, 'cbc', self::rij, self::algo);

        // Verify HMAC before decrypting... return false if corrupt.
        if (!Strcmp::equals($verify, $chksum)) {
            return false;
        }

        // Decrypt message and return
        return openssl_decrypt($message, self::cipher, $key, 1, $iv);
    }

    /**
     * Encrypt plaintext
     * 
     * @param string $plaintext Plaintext string to encrypt.
     * @param string $key       Key used to encrypt data.
     * 
     * @return string 
     */
    public static function encrypt($plaintext, $key)
    {
        // Normalize (de/en)cryption key (by-ref)
        self::_init($key, self::rij, 'cbc', self::algo);

        // Generate IV of appropriate size.
        $iv = Random::get(self::ivsize);

        // Encrypt the plaintext
        $message = openssl_encrypt($plaintext, self::cipher, $key, 1, $iv);

        // Create the cypher text prefix (iv + checksum)
        $prefix = $iv . self::_checksum($message, $iv, $key, 'cbc', self::rij, self::algo);

        // Return prefix + cyphertext
        return $prefix . $message;
    }

}

{'fraction_non_alphanumeric': 0.12849358410968537, 'fraction_numerical': 0.00615222358938302, 'mean_word_length': 2.1823266219239374, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 4, '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': '5474633', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1730, 'n_words': 492}