<commit_before><?php

namespace SAML2\Certificate;

use RobRichards\XMLSecLibs\XMLSecurityKey;
use SAML2\Configuration\DecryptionProvider;
use SAML2\Configuration\PrivateKey as PrivateKeyConfiguration;
use SAML2\Utilities\ArrayCollection;
use SAML2\Utilities\File;

class PrivateKeyLoader
{
    /**
     * Loads a private key based on the configuration given.
     *
     * @param \SAML2\Configuration\PrivateKey $key
     *
     * @return \SAML2\Certificate\PrivateKey
     */
    public function loadPrivateKey(PrivateKeyConfiguration $key)
    {
        $privateKey = File::getFileContents($key->getFilePath());

        return PrivateKey::create($privateKey, $key->getPassPhrase());
    }

    /**
     * @param \SAML2\Configuration\DecryptionProvider $identityProvider
     * @param \SAML2\Configuration\DecryptionProvider $serviceProvider
     *
     * @return \SAML2\Utilities\ArrayCollection
     * @throws \Exception
     */
    public function loadDecryptionKeys(
        DecryptionProvider $identityProvider,
        DecryptionProvider $serviceProvider
    ) {
        $decryptionKeys = new ArrayCollection();

        $senderSharedKey = $identityProvider->getSharedKey();
        if ($senderSharedKey) {
            $key = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
            $key->loadKey($senderSharedKey);
            $decryptionKeys->add($key);

            return $decryptionKeys;
        }

        $newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
        if ($newPrivateKey instanceof PrivateKey) {
            $loadedKey = $this->loadPrivateKey($newPrivateKey);
            $decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
        }

        $privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
        $loadedKey  = $this->loadPrivateKey($privateKey);
        $decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));

        return $decryptionKeys;
    }

    /**
     * @param \SAML2\Certificate\PrivateKey $privateKey
     *
     * @return XMLSecurityKey<commit_msg>Fix reference to PrivateKey where it should be PrivateKeyConfiguration in PrivateKeyLoader.<commit_after><?php

namespace SAML2\Certificate;

use RobRichards\XMLSecLibs\XMLSecurityKey;
use SAML2\Configuration\DecryptionProvider;
use SAML2\Configuration\PrivateKey as PrivateKeyConfiguration;
use SAML2\Utilities\ArrayCollection;
use SAML2\Utilities\File;

class PrivateKeyLoader
{
    /**
     * Loads a private key based on the configuration given.
     *
     * @param \SAML2\Configuration\PrivateKey $key
     *
     * @return \SAML2\Certificate\PrivateKey
     */
    public function loadPrivateKey(PrivateKeyConfiguration $key)
    {
        $privateKey = File::getFileContents($key->getFilePath());

        return PrivateKey::create($privateKey, $key->getPassPhrase());
    }

    /**
     * @param \SAML2\Configuration\DecryptionProvider $identityProvider
     * @param \SAML2\Configuration\DecryptionProvider $serviceProvider
     *
     * @return \SAML2\Utilities\ArrayCollection
     * @throws \Exception
     */
    public function loadDecryptionKeys(
        DecryptionProvider $identityProvider,
        DecryptionProvider $serviceProvider
    ) {
        $decryptionKeys = new ArrayCollection();

        $senderSharedKey = $identityProvider->getSharedKey();
        if ($senderSharedKey) {
            $key = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
            $key->loadKey($senderSharedKey);
            $decryptionKeys->add($key);

            return $decryptionKeys;
        }

        $newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
        if ($newPrivateKey instanceof PrivateKeyConfiguration) {
            $loadedKey = $this->loadPrivateKey($newPrivateKey);
            $decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));
        }

        $privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
        $loadedKey  = $this->loadPrivateKey($privateKey);
        $decryptionKeys->add($this->convertPrivateKeyToRsaKey($loadedKey));

        return $decryptionKeys;
    }

    /**
     * @param \SAML2\Certificate\PrivateKey $privateKey
     *
     * @return XMLSecurityKey
{'fraction_non_alphanumeric': 0.10640279394644936, 'fraction_numerical': 0.006519208381839348, 'mean_word_length': 3.3132530120481927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '2639044', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1160, 'n_words': 195}