<commit_before><?php

namespace SpomkyLabs\Jose\Tests;

use SpomkyLabs\Jose\JWK;
use Base64Url\Base64Url;
use SpomkyLabs\Jose\Algorithm\KeyEncryption\RSA15;
use SpomkyLabs\Jose\Algorithm\KeyEncryption\RSAOAEP;
use SpomkyLabs\Jose\Algorithm\KeyEncryption\RSAOAEP256;

/**
 * Class RSAKeyEncryptionTest
 * @package SpomkyLabs\Jose\Tests
 */
class RSAKeyEncryptionTest extends TestCase
{
    /**
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage The key is not valid
     */
    public function testInvalidKey()
    {
        $key  = new JWK();
        $key->setValue("kty", "EC");

        $rsa1_5 = new RSA15();

        $header = array();
        $data = "Je suis Charlie";

        $rsa1_5->encryptKey($key, $data, $header);
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.2
     */
    public function testRSA1_5EncryptionAndDecryption()
    {
        $header = array();
        $jwk = new JWK();
        $jwk->setValues(array(
            "kty" => "RSA",
            "n"   => "<KEY>",
            "e"   => "AQAB",
            "d"   => "<KEY>",
            "p"   => "<KEY>",
            "q"   => "<KEY>",
            "dp"  => "<KEY>",
            "dq"  => "<KEY>",
            "qi"  => "<KEY>",
        ));

        $cek = [4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207];
        foreach ($cek as $key => $value) {
            $cek[$key] = str_pad(dechex($value), 2, "0", STR_PAD_LEFT);
        }
        $cek = hex2bin(implode("", $cek));

        $from_specification = Base64Url::decode("<KEY>");

        $rsa1_5 = new RSA15();
        $encrypted = $rsa1_5->encryptKey($jwk, $cek, $header);

        $this->assertEquals($cek, $rsa1_5->decryptKey($jwk, $encrypted, $header));
        $this->assertEquals($cek, $rsa1_5->decryptKey($jwk, $from_specification, $header));
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.1
     */
    public function testRSAOAEPEncryptionAndDecryption()
    {
        $header = array();
        $jwk = new JWK();
        $jwk->setValues(array(
            "kty" => "RSA",
            "n"   => "<KEY>",
            "e"   => "AQAB",
            "d"   => "<KEY>",
            "p"   => "<KEY>",
            "q"   => "<KEY>",
            "dp"  => "<KEY>",
            "dq"  => "<KEY>",
            "qi"  => "VIMpMYbPf47dT1w_zDUXfPimsSegnMOA1zTaX7aGk_8urY6R8-ZW1FxU7AlWAyLWybqq6t16VFd7hQd0y6flUK4SlOydB61gwanOsXGOAOv82cHq0E3eL4HrtZkUuKvnPrMnsUUFlfUdybVzxyjz9JF_XyaY14ardLSjf4L_FNY",
        ));

        $cek = [177, 161, 244, 128, 84, 143, 225, 115, 63, 180, 3, 255, 107, 154, 212, 246, 138, 7, 110, 91, 112, 46, 34, 105, 47, 130, 203, 46, 122, 234, 64, 252];
        foreach ($cek as $key => $value) {
            $cek[$key] = str_pad(dechex($value), 2, "0", STR_PAD_LEFT);
        }
        $cek = hex2bin(implode("", $cek));

        $from_specification = Base64Url::decode("<KEY>");

        $rsa_oaep = new RSAOAEP();
        $encrypted = $rsa_oaep->encryptKey($jwk, $cek, $header);

        $this->assertEquals($cek, $rsa_oaep->decryptKey($jwk, $encrypted, $header));
        $this->assertEquals($cek, $rsa_oaep->decryptKey($jwk, $from_specification, $header));
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.1
     */
    public function testRSAOAEP256EncryptionAndDecryption()
    {
        $header = array();
        $jwk = new JWK();
        $jwk->setValues(array(
            "kty" => "RSA",
            "n"   => "<KEY>",
            "e"   => "AQAB",
            "d"   => "<KEY>",
            "p"   => "<KEY>",
            "q"   => "<KEY>",
            "dp"  => "<KEY>",
            "dq"  => "<KEY>",
            "qi"  => "<KEY>",
        ));

        $cek = [177, 161, 244, 128, 84, 143, 225, 115, 63, 180, 3, 255, 107, 154, 212, 246, 138, 7, 110, 91, 112, 46, 34, 105, 47, 130, 203, 46, 122, 234, 64, 252];
        foreach ($cek as $key => $value) {
            $cek[$key] = str_pad(dechex($value), 2, "0", STR_PAD_LEFT);
        }
        $cek = hex2bin(implode("", $cek));

        $rsa_oaep_256 = new RSAOAEP256();
        $encrypted = $rsa_oaep_256->encryptKey($jwk, $cek, $header);

        $this->assertEquals($cek, $rsa_oaep_256->decryptKey($jwk, $encrypted, $header));
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.1
     */
    public function testLoadJWK1()
    {
        $loader = $this->getLoader();

        $result = $loader->load("<KEY>");

        $this->assertInstanceOf("Jose\JWEInterface", $result);
        $this->assertEquals("The true sign of intelligence is not knowledge but imagination.", $result->getPayload());
        $this->assertEquals("RSA-OAEP", $result->getAlgorithm());
        $this->assertEquals("A256GCM", $result->getEncryptionAlgorithm());
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.2
     */
    public function testLoadJWK2()
    {
        $loader = $this->getLoader();

        $result = $loader->load("<KEY>");

        $this->assertInstanceOf("Jose\JWEInterface", $result);
        $this->assertEquals("Live long and prosper.", $result->getPayload());
        $this->assertEquals("RSA1_5", $result->getAlgorithm());
        $this->assertEquals("A128CBC-HS256", $result->getEncryptionAlgorithm());
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.3
     */
    public function testLoadJWK3()
    {
        $loader = $this->getLoader();

        $result = $loader->load("<KEY>");

        $this->assertInstanceOf("Jose\JWEInterface", $result);
        $this->assertEquals("Live long and prosper.", $result->getPayload());
        $this->assertEquals("A128KW", $result->getAlgorithm());
        $this->assertEquals("A128CBC-HS256", $result->getEncryptionAlgorithm());
    }
}<commit_msg>Bug fixed<commit_after><?php

namespace SpomkyLabs\Jose\Tests;

use SpomkyLabs\Jose\JWK;
use Base64Url\Base64Url;
use SpomkyLabs\Jose\Algorithm\KeyEncryption\RSA15;
use SpomkyLabs\Jose\Algorithm\KeyEncryption\RSAOAEP;
use SpomkyLabs\Jose\Algorithm\KeyEncryption\RSAOAEP256;

/**
 * Class RSAKeyEncryptionTest
 * @package SpomkyLabs\Jose\Tests
 */
class RSAKeyEncryptionTest extends TestCase
{
    /**
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage The key is not valid
     */
    public function testInvalidKey()
    {
        $key  = new JWK();
        $key->setValue("kty", "EC");

        $rsa1_5 = new RSA15();

        $header = array();
        $data = "Je suis Charlie";

        $rsa1_5->encryptKey($key, $data, $header);
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.2
     */
    public function testRSA1_5EncryptionAndDecryption()
    {
        $header = array();
        $jwk = new JWK();
        $jwk->setValues(array(
            "kty" => "RSA",
            "n"   => "<KEY>",
            "e"   => "AQAB",
            "d"   => "<KEY>",
            "p"   => "<KEY>",
            "q"   => "<KEY>",
            "dp"  => "<KEY>",
            "dq"  => "<KEY>",
            "qi"  => "<KEY>",
        ));

        $cek = [4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207];
        foreach ($cek as $key => $value) {
            $cek[$key] = str_pad(dechex($value), 2, "0", STR_PAD_LEFT);
        }
        $cek = hex2bin(implode("", $cek));

        $from_specification = Base64Url::decode("UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-kFm1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKxGHZ7PcHALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3YvkkysZIFNPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPhcCdZ6XDP0_F8rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPgwCp6X-nZZd9OHBv-B3oWh2TbqmScqXMR4gp_A");

        $rsa1_5 = new RSA15();
        $encrypted = $rsa1_5->encryptKey($jwk, $cek, $header);

        $this->assertEquals($cek, $rsa1_5->decryptKey($jwk, $encrypted, $header));
        $this->assertEquals($cek, $rsa1_5->decryptKey($jwk, $from_specification, $header));
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.1
     */
    public function testRSAOAEPEncryptionAndDecryption()
    {
        $header = array();
        $jwk = new JWK();
        $jwk->setValues(array(
            "kty" => "RSA",
            "n"   => "<KEY>",
            "e"   => "AQAB",
            "d"   => "<KEY>",
            "p"   => "<KEY>",
            "q"   => "<KEY>",
            "dp"  => "<KEY>",
            "dq"  => "<KEY>",
            "qi"  => "<KEY>",
        ));

        $cek = [177, 161, 244, 128, 84, 143, 225, 115, 63, 180, 3, 255, 107, 154, 212, 246, 138, 7, 110, 91, 112, 46, 34, 105, 47, 130, 203, 46, 122, 234, 64, 252];
        foreach ($cek as $key => $value) {
            $cek[$key] = str_pad(dechex($value), 2, "0", STR_PAD_LEFT);
        }
        $cek = hex2bin(implode("", $cek));

        $from_specification = Base64Url::decode("<KEY>");

        $rsa_oaep = new RSAOAEP();
        $encrypted = $rsa_oaep->encryptKey($jwk, $cek, $header);

        $this->assertEquals($cek, $rsa_oaep->decryptKey($jwk, $encrypted, $header));
        $this->assertEquals($cek, $rsa_oaep->decryptKey($jwk, $from_specification, $header));
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.1
     */
    public function testRSAOAEP256EncryptionAndDecryption()
    {
        $header = array();
        $jwk = new JWK();
        $jwk->setValues(array(
            "kty" => "RSA",
            "n"   => "<KEY>",
            "e"   => "AQAB",
            "d"   => "<KEY>",
            "p"   => "<KEY>",
            "q"   => "<KEY>",
            "dp"  => "<KEY>",
            "dq"  => "<KEY>",
            "qi"  => "<KEY>",
        ));

        $cek = [177, 161, 244, 128, 84, 143, 225, 115, 63, 180, 3, 255, 107, 154, 212, 246, 138, 7, 110, 91, 112, 46, 34, 105, 47, 130, 203, 46, 122, 234, 64, 252];
        foreach ($cek as $key => $value) {
            $cek[$key] = str_pad(dechex($value), 2, "0", STR_PAD_LEFT);
        }
        $cek = hex2bin(implode("", $cek));

        $rsa_oaep_256 = new RSAOAEP256();
        $encrypted = $rsa_oaep_256->encryptKey($jwk, $cek, $header);

        $this->assertEquals($cek, $rsa_oaep_256->decryptKey($jwk, $encrypted, $header));
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.1
     */
    public function testLoadJWK1()
    {
        if (!$this->isCryptoExtensionAvailable()) {
            $this->markTestSkipped("PHP Crypto extension not available.");

            return;
        }
        $loader = $this->getLoader();

        $result = $loader->load("<KEY>");

        $this->assertInstanceOf("Jose\JWEInterface", $result);
        $this->assertEquals("The true sign of intelligence is not knowledge but imagination.", $result->getPayload());
        $this->assertEquals("RSA-OAEP", $result->getAlgorithm());
        $this->assertEquals("A256GCM", $result->getEncryptionAlgorithm());
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.2
     */
    public function testLoadJWK2()
    {
        $loader = $this->getLoader();

        $result = $loader->load("<KEY>");

        $this->assertInstanceOf("Jose\JWEInterface", $result);
        $this->assertEquals("Live long and prosper.", $result->getPayload());
        $this->assertEquals("RSA1_5", $result->getAlgorithm());
        $this->assertEquals("A128CBC-HS256", $result->getEncryptionAlgorithm());
    }

    /**
     * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.3
     */
    public function testLoadJWK3()
    {
        $loader = $this->getLoader();

        $result = $loader->load("<KEY>");

        $this->assertInstanceOf("Jose\JWEInterface", $result);
        $this->assertEquals("Live long and prosper.", $result->getPayload());
        $this->assertEquals("A128KW", $result->getAlgorithm());
        $this->assertEquals("A128CBC-HS256", $result->getEncryptionAlgorithm());
    }

    /**
     * @return bool
     */
    private function isCryptoExtensionAvailable()
    {
        return class_exists("\Crypto\Cipher");
    }
}
{'fraction_non_alphanumeric': 0.1862924385783573, 'fraction_numerical': 0.061699928440804644, 'mean_word_length': 2.4235166031573216, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 205, 'https://': 12, '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': '338684', 'n_tokens_mistral': 5349, 'n_tokens_neox': 4551, 'n_words': 786}