<commit_before><?php namespace App\Utils\Data;

// The MIT License (MIT)

// Copyright (c) 2015 <NAME>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Don't squawk.
!defined('BANTAM') ? exit() : null;

use \App\Utils\User\Session;
use \App\Utils\Misc\Config;
use \App\Utils\Data\Prelims;

/**
* Provides basic encryption, decryption, hashing and signing abilities.
* @package App\Utils\Data
* @author <NAME> <<EMAIL>>
* @license http://opensource.org/licenses/MIT MIT License (MIT)
*/
class Crypto {
    const cipher_type     = MCRYPT_RIJNDAEL_256;
    const cipher_mode     = MCRYPT_MODE_CBC;
    const salt_bytes_size = 32;
    const password_algo   = PASSWORD_BCRYPT;
    const salt_error      = 'Salt required, please pass it in, start a session or set \'bantam_app_secret_key\' to a secure value in Config.';

    /**
    * Encrypt a string and return the base64 encoded result. If the size
    * of the data is not n * blocksize, the data will be padded with '\0'.
    * @param string $string The string to encrypt.
    * @return string The base64 encoded encrypted string.
    */
    public static function encrypt($string, $salt=null) {
        if($salt === null) {
            $salt = Config::get('bantam_app_secret_key', null);
            if($salt === null && !Session::hasBegun()) {
                throw new \ErrorException(self::salt_error);
            }

            if(Session::hasBegun() && Session::valid()) {
                $salt = Session::get('salt');
                if($salt === null) {
                    $salt = openssl_random_pseudo_bytes(self::salt_bytes_size);
                    Session::set('salt', $salt);
                }
            }
        }

        // $salt = openssl_random_pseudo_bytes(self::salt_bytes_size);
        $iv_size = mcrypt_get_iv_size(self::cipher_type, self::cipher_mode);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

        $encrypted_string = mcrypt_encrypt(self::cipher_type, $salt, $string, self::cipher_mode, $iv);
        return base64_encode($iv.$encrypted_string);
    }

    /**
    * Decrypt a string and return the plaintext result.
    * @param string $string The string to decrypt.
    * @return string The base64 decoded decrypted string.
    */
    public static function decrypt($string, $salt=null) {
        if($salt === null) {
            $salt = Config::get('bantam_app_secret_key', null);
            if($salt === null && !Session::hasBegun()) {
                throw new \ErrorException(self::salt_error);
            }

            if(Session::hasBegun() && Session::valid()) {
                $salt = Session::get('salt');
                if($salt === null) {
                    $salt = openssl_random_pseudo_bytes(self::salt_bytes_size);
                    Session::set('salt', $salt);
                }
            }
        }

        $raw_bytes = base64_decode($string);
        $iv_size = mcrypt_get_iv_size(self::cipher_type, self::cipher_mode);
        $iv = substr($raw_bytes, 0, $iv_size);

        $encrypted_string = substr($raw_bytes, $iv_size);
        $string = mcrypt_decrypt(self::cipher_type, $salt, $encrypted_string, self::cipher_mode, $iv);
        return $string;
    }

    /**
    * Sign the given string and return the same string with the signature
    * appended with a '--' seperator. This function requires you either have
    * a unique, application specific 'key' set in your Config, or that you have
    * started a Session before this function is called. If neither of these
    * conditions are met, an ErrorException will be thrown.
    * @param string $string The string to sign.
    * @return string The signed string.
    */
    public static function sign($string) {
        $salt = Config::get('bantam_app_secret_key', null);
        if($salt === null && !Session::hasBegun()) {
            throw new \ErrorException('You need to set \'key\' in your Config or start a Session.');
        }

        if(Session::hasBegun() && Session::isValid()) {
            $salt = Session::get('salt');
            if($salt === null) {
                $salt = openssl_random_pseudo_bytes(self::salt_bytes_size);
                Session::set('salt', $salt);<commit_msg>Renamed misleading salt variable to key.<commit_after><?php namespace App\Utils\Data;

// The MIT License (MIT)

// Copyright (c) 2015 <NAME>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Don't squawk.
!defined('BANTAM') ? exit() : null;

use \App\Utils\User\Session;
use \App\Utils\Misc\Config;
use \App\Utils\Data\Prelims;

/**
* Provides basic encryption, decryption, hashing and signing abilities.
* @package App\Utils\Data
* @author <NAME> <<EMAIL>>
* @license http://opensource.org/licenses/MIT MIT License (MIT)
*/
class Crypto {
    const cipher_type     = MCRYPT_RIJNDAEL_256;
    const cipher_mode     = MCRYPT_MODE_CBC;
    const salt_bytes_size = 32;
    const password_algo   = PASSWORD_BCRYPT;
    const salt_error      = 'Salt required, please pass it in, start a session or set \'bantam_app_secret_key\' to a secure value in Config.';

    /**
    * Encrypt a string and return the base64 encoded result. If the size
    * of the data is not n * blocksize, the data will be padded with '\0'.
    * @param string $string The string to encrypt.
    * @return string The base64 encoded encrypted string.
    */
    public static function encrypt($string, $key=null) {
        if($key === null) {
            $key = Config::get('bantam_app_secret_key', null);
            if($key === null && !Session::hasBegun()) {
                throw new \ErrorException(self::salt_error);
            }

            if(Session::hasBegun() && Session::valid()) {
                $key = Session::get('salt');
                if($key === null) {
                    $key = openssl_random_pseudo_bytes(self::salt_bytes_size);
                    Session::set('salt', $key);
                }
            }
        }

        // $key = openssl_random_pseudo_bytes(self::salt_bytes_size);
        $iv_size = mcrypt_get_iv_size(self::cipher_type, self::cipher_mode);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

        $encrypted_string = mcrypt_encrypt(self::cipher_type, $key, $string, self::cipher_mode, $iv);
        return base64_encode($iv.$encrypted_string);
    }

    /**
    * Decrypt a string and return the plaintext result.
    * @param string $string The string to decrypt.
    * @return string The base64 decoded decrypted string.
    */
    public static function decrypt($string, $key=null) {
        if($key === null) {
            $key = Config::get('bantam_app_secret_key', null);
            if($key === null && !Session::hasBegun()) {
                throw new \ErrorException(self::salt_error);
            }

            if(Session::hasBegun() && Session::valid()) {
                $key = Session::get('salt');
                if($key === null) {
                    $key = openssl_random_pseudo_bytes(self::salt_bytes_size);
                    Session::set('salt', $key);
                }
            }
        }

        $raw_bytes = base64_decode($string);
        $iv_size = mcrypt_get_iv_size(self::cipher_type, self::cipher_mode);
        $iv = substr($raw_bytes, 0, $iv_size);

        $encrypted_string = substr($raw_bytes, $iv_size);
        $string = mcrypt_decrypt(self::cipher_type, $key, $encrypted_string, self::cipher_mode, $iv);
        return $string;
    }

    /**
    * Sign the given string and return the same string with the signature
    * appended with a '--' seperator. This function requires you either have
    * a unique, application specific 'key' set in your Config, or that you have
    * started a Session before this function is called. If neither of these
    * conditions are met, an ErrorException will be thrown.
    * @param string $string The string to sign.
    * @return string The signed string.
    */
    public static function sign($string) {
        $salt = Config::get('bantam_app_secret_key', null);
        if($salt === null && !Session::hasBegun()) {
            throw new \ErrorException('You need to set \'key\' in your Config or start a Session.');
        }

        if(Session::hasBegun() && Session::isValid()) {
            $salt = Session::get('salt');
            if($salt === null) {
                $salt = openssl_random_pseudo_bytes(self::salt_bytes_size);
                Session::set('salt', $salt);
{'fraction_non_alphanumeric': 0.11446360153256704, 'fraction_numerical': 0.004022988505747126, 'mean_word_length': 2.7275972866833276, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '2720460', 'n_tokens_mistral': 3126, 'n_tokens_neox': 2900, 'n_words': 978}