<commit_before><?php
/**
 * This file is part of the Global Trading Technologies Ltd crypt-bundle package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * (c) fduch <<EMAIL>>
 *
 * @date 18.12.15
 */

namespace Gtt\Bundle\CryptBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Zend\Crypt\PublicKey\Rsa;
use Crypto;

/**
 * Bundle configuration
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode    = $treeBuilder->root('gtt_crypt');

        $rootNode
            ->fixXmlConfig('cryptor')
            ->children()
                ->append($this->createCryptorsNode())
            ->end();

        return $treeBuilder;
    }

    /**
     * Create cryptors configuration
     *
     * @return ArrayNodeDefinition|NodeDefinition
     */
    private function createCryptorsNode()
    {
        $builder = new TreeBuilder();
        $cryptorsNode = $builder->root('cryptors');
        $cryptorsNode
            ->validate()
            ->always()
            ->then(function($cryptors) {
                // check that cryptor names are unique
                if (!$cryptors) {
                    return array();
                }
                $usedNames = array();
                foreach ($cryptors as $type => $typedCryptors) {
                    foreach ($typedCryptors as $name => $cryptorConfig) {
                        if (in_array($name, $usedNames)) {
                            throw new InvalidConfigurationException(
                                sprintf("Cryptor names should be unique. '%s' name is duplicated", $name));
                        } else {
                            $usedNames[] = $name;
                        }
                    }
                }
                return $cryptors;
            })
            ->end();
        if (class_exists(Rsa::class, true)) {
            $cryptorsNode->append($this->createRsaNode());
        }
        if (class_exists(Crypto::class, true)) {
            $cryptorsNode->append($this->createAesNode());
        }
        return $cryptorsNode;
    }

    /**
     * Create section of RSA configuration
     *
     * @return ArrayNodeDefinition|NodeDefinition
     */
    private function createRsaNode()
    {
        $builder = new TreeBuilder();
        $rsaNode = $builder->root('rsa');
        $rsaNode
            ->info(
                'Public-key (asymmetric) cryptosystem. ' .
                'Provided by and require the zendframework/zend-crypt package'
            )
            ->useAttributeAsKey('name')
            ->prototype('array')
                ->children()
                    ->scalarNode('private_key')->end()
                    ->scalarNode('pass_phrase')->end()
                    ->scalarNode('public_key')->end()
                    ->scalarNode('binary_output')->defaultTrue()->end()
                    ->scalarNode('hash_algorithm')->defaultValue('sha1')->end()
                ->end()
            ->end();
        return $rsaNode;
    }

    /**
     * Create section of AES configuration
     *
     * @return ArrayNodeDefinition|NodeDefinition
     */
    private function createAesNode()
    {
        $builder = new TreeBuilder();
        $aesNode = $builder->root('aes');
        $aesNode
            ->info(sprintf(
                'Symmetric encryption (%s, %s mode and are authenticated with HMAC-%s). ' .
                'Provided by and require the defuse/php-encryption package.',
                strtoupper(Crypto::CIPHER),
                strtoupper(Crypto::CIPHER_MODE),
                strtoupper(Crypto::HASH_FUNCTION)
            ))
            ->useAttributeAsKey('name')
            ->prototype('array')
            ->children()
                ->scalarNode('key_size')
                    ->info('Parameter should match the version of the library: 128 for 1.x or 256 for 2.x. ')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->validate()<commit_msg>remove unused rsa hash_algorithm config<commit_after><?php
/**
 * This file is part of the Global Trading Technologies Ltd crypt-bundle package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * (c) fduch <<EMAIL>>
 *
 * @date 18.12.15
 */

namespace Gtt\Bundle\CryptBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Zend\Crypt\PublicKey\Rsa;
use Crypto;

/**
 * Bundle configuration
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode    = $treeBuilder->root('gtt_crypt');

        $rootNode
            ->fixXmlConfig('cryptor')
            ->children()
                ->append($this->createCryptorsNode())
            ->end();

        return $treeBuilder;
    }

    /**
     * Create cryptors configuration
     *
     * @return ArrayNodeDefinition|NodeDefinition
     */
    private function createCryptorsNode()
    {
        $builder = new TreeBuilder();
        $cryptorsNode = $builder->root('cryptors');
        $cryptorsNode
            ->validate()
            ->always()
            ->then(function($cryptors) {
                // check that cryptor names are unique
                if (!$cryptors) {
                    return array();
                }
                $usedNames = array();
                foreach ($cryptors as $type => $typedCryptors) {
                    foreach ($typedCryptors as $name => $cryptorConfig) {
                        if (in_array($name, $usedNames)) {
                            throw new InvalidConfigurationException(
                                sprintf("Cryptor names should be unique. '%s' name is duplicated", $name));
                        } else {
                            $usedNames[] = $name;
                        }
                    }
                }
                return $cryptors;
            })
            ->end();
        if (class_exists(Rsa::class, true)) {
            $cryptorsNode->append($this->createRsaNode());
        }
        if (class_exists(Crypto::class, true)) {
            $cryptorsNode->append($this->createAesNode());
        }
        return $cryptorsNode;
    }

    /**
     * Create section of RSA configuration
     *
     * @return ArrayNodeDefinition|NodeDefinition
     */
    private function createRsaNode()
    {
        $builder = new TreeBuilder();
        $rsaNode = $builder->root('rsa');
        $rsaNode
            ->info(
                'Public-key (asymmetric) cryptosystem. ' .
                'Provided by and require the zendframework/zend-crypt package'
            )
            ->useAttributeAsKey('name')
            ->prototype('array')
                ->children()
                    ->scalarNode('private_key')->end()
                    ->scalarNode('pass_phrase')->end()
                    ->scalarNode('public_key')->end()
                    ->scalarNode('binary_output')->defaultTrue()->end()
                ->end()
            ->end();
        return $rsaNode;
    }

    /**
     * Create section of AES configuration
     *
     * @return ArrayNodeDefinition|NodeDefinition
     */
    private function createAesNode()
    {
        $builder = new TreeBuilder();
        $aesNode = $builder->root('aes');
        $aesNode
            ->info(sprintf(
                'Symmetric encryption (%s, %s mode and are authenticated with HMAC-%s). ' .
                'Provided by and require the defuse/php-encryption package.',
                strtoupper(Crypto::CIPHER),
                strtoupper(Crypto::CIPHER_MODE),
                strtoupper(Crypto::HASH_FUNCTION)
            ))
            ->useAttributeAsKey('name')
            ->prototype('array')
            ->children()
                ->scalarNode('key_size')
                    ->info('Parameter should match the version of the library: 128 for 1.x or 256 for 2.x. ')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->validate()
{'fraction_non_alphanumeric': 0.1174496644295302, 'fraction_numerical': 0.003243847874720358, 'mean_word_length': 1.7118592659993934, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 96, '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': '3852474', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2205, 'n_words': 490}