<commit_before><?php // vim:ts=4:sts=4:sw=4:et:

namespace PHPCrypt;

class SimpleTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Simple
     */
    private $_instance;
    private $_encryptionKey = '<KEY>';
    private $_macKey = '<KEY>';

    public function setUp() {
        $this->_instance = new Simple($this->_encryptionKey, $this->_macKey);
    }

    public function testCanEncryptPlaintext() {
        $ciphertext = $this->_instance->encrypt('FooBar ', '<KEY>');
        
        $this->assertEquals(
            '<KEY>',
            $ciphertext
        );
    }
    
    public function testCanDecryptCiphertext() {
        $plaintext = $this->_instance->decrypt(
            '<KEY>RHJpeUlMOVE9'
        );
        
        $this->assertEquals('FooBar ', $plaintext);
    }
    
    public function testDecryptingSignedCiphertextWithUnknownConstructionThrowsException() {
        $this->setExpectedException('RunTimeException', 'Unknown construction, "rjd-256-hmac-sha256/128"');
        $this->_instance->decrypt(
            'cmpkLTI1Ni1obWFjLXNoYTI1Ni8xMjh8bEF1Q1U3ZnQ1dG5IUEtXUmpGMUlLVjRKNlY5L2VDR1FJaXNIWmZ1cU10YS8zZ0pnN0loSGd4dmFVWTZpc1J5UGNScSt4L3JRZm5QeFkvQTFYcVkybkE9PXw2TUJCQ0tCYlljK2FXTHBuazFNUVZXMmpNNUpuejVvSGZYbkRyaXlJTDlRPQ=='
        );
    }

    /**
     * Encrypts the same string with randomized IVs and flips a single bit of
     * the ciphertext.
     */
    public function invalidCiphertextData() {
        $this->setUp();
        $invalidCiphertexts = array();

        for ($x = 0; $x < 100; $x++) {
            $signedCiphertext = $this->_instance->encrypt('Randomize this with new IVs');
            $signedCiphertext = base64_decode($signedCiphertext);
            list($construction, $encodedCiphertext, $encodedSignature) = explode('|', $signedCiphertext);
            $ciphertext = base64_decode($encodedCiphertext);
            $randomByte = rand(1, strlen($ciphertext));
            $mask = str_repeat("\x00", $randomByte -1) . "\x01" . str_repeat("\x00", strlen($ciphertext) - $randomByte);
            
            // SANITY CHECK: If this mask is removed, this test should fail every 
            // single run because the ciphertext should match.
            $invalidCiphertext = $ciphertext ^ $mask;

            // printf("Ciphertext:         %s\n", bin2hex($ciphertext));
            // printf("Mask:               %s\n", bin2hex($mask));
            // printf("Invalid Ciphertext: %s\n", bin2hex($invalidCiphertext));
            
            $encodedInvalidCiphertext = base64_encode($invalidCiphertext);
            $invalidCiphertexts[] = array(base64_encode($construction . '|' . $encodedInvalidCiphertext . '|' . $encodedSignature));
        }

        return $invalidCiphertexts;
    }

    /**
     * @dataProvider invalidCiphertextData
     */
    public function testDecryptingInvalidCiphertextThrowsException($signedCiphertext) {
        $this->setExpectedException('RuntimeException', 'Invalid signature');
        $this->_instance->decrypt($signedCiphertext);
    }
    
    public function testEncryptedDataCanBeDecrypted() {
        $plaintext = 'Something';
        $ciphertext = $this->_instance->encrypt('Something');
        $this->assertEquals($plaintext, $this->_instance->decrypt($ciphertext));
    }

    public function invalidKeyData() {
        return array(
            array(null),
            array(''),
            array('Foo'),
            array('<KEY>'),
            array('<KEY>'),
            array('<KEY>),
        );
    }

    /**
     * @dataProvider invalidKeyData
     */
    public function testInvalidEncryptionKeyThrowsException($invalidKey) {
        $this->setExpectedException('InvalidArgumentException');
        new Simple($invalidKey, $this->_macKey);
    }
    
    /**
     * @dataProvider invalidKeyData
     */
    public function testInvalidMacKeySizeThrowsException($invalidKey) {
        $this->setExpectedException('InvalidArgumentException');
        new Simple($this->_encryptionKey, $invalidKey);
    }

    public function testEncryptWithInvalidIvLengthThrowsException() {
        $this->setExpectedException('InvalidArgumentException');
<commit_msg>Test for specific Exception messages<commit_after><?php // vim:ts=4:sts=4:sw=4:et:

namespace PHPCrypt;

class SimpleTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Simple
     */
    private $_instance;
    private $_encryptionKey = '<KEY>';
    private $_macKey = '<KEY>';

    public function setUp() {
        $this->_instance = new Simple($this->_encryptionKey, $this->_macKey);
    }

    public function testCanEncryptPlaintext() {
        $ciphertext = $this->_instance->encrypt('FooBar ', 'lAuCU7ft5tnHPKWRjF1IKV4J6V9/eCGQIisHZfuqMtY=');
        
        $this->assertEquals(
            '<KEY>',
            $ciphertext
        );
    }
    
    public function testCanDecryptCiphertext() {
        $plaintext = $this->_instance->decrypt(
            '<KEY>'
        );
        
        $this->assertEquals('FooBar ', $plaintext);
    }
    
    public function testDecryptingSignedCiphertextWithUnknownConstructionThrowsException() {
        $this->setExpectedException('RunTimeException', 'Unknown construction, "rjd-256-hmac-sha256/128"');
        $this->_instance->decrypt(
            'cmpkLTI1Ni1obWFjLXNoYTI1Ni8xMjh8bEF1Q1U3ZnQ1dG5IUEtXUmpGMUlLVjRKNlY5L<KEY>
        );
    }

    /**
     * Encrypts the same string with randomized IVs and flips a single bit of
     * the ciphertext.
     */
    public function invalidCiphertextData() {
        $this->setUp();
        $invalidCiphertexts = array();

        for ($x = 0; $x < 100; $x++) {
            $signedCiphertext = $this->_instance->encrypt('Randomize this with new IVs');
            $signedCiphertext = base64_decode($signedCiphertext);
            list($construction, $encodedCiphertext, $encodedSignature) = explode('|', $signedCiphertext);
            $ciphertext = base64_decode($encodedCiphertext);
            $randomByte = rand(1, strlen($ciphertext));
            $mask = str_repeat("\x00", $randomByte -1) . "\x01" . str_repeat("\x00", strlen($ciphertext) - $randomByte);
            
            // SANITY CHECK: If this mask is removed, this test should fail every 
            // single run because the ciphertext should match.
            $invalidCiphertext = $ciphertext ^ $mask;

            // printf("Ciphertext:         %s\n", bin2hex($ciphertext));
            // printf("Mask:               %s\n", bin2hex($mask));
            // printf("Invalid Ciphertext: %s\n", bin2hex($invalidCiphertext));
            
            $encodedInvalidCiphertext = base64_encode($invalidCiphertext);
            $invalidCiphertexts[] = array(base64_encode($construction . '|' . $encodedInvalidCiphertext . '|' . $encodedSignature));
        }

        return $invalidCiphertexts;
    }

    /**
     * @dataProvider invalidCiphertextData
     */
    public function testDecryptingInvalidCiphertextThrowsException($signedCiphertext) {
        $this->setExpectedException('RuntimeException', 'Invalid signature');
        $this->_instance->decrypt($signedCiphertext);
    }
    
    public function testEncryptedDataCanBeDecrypted() {
        $plaintext = 'Something';
        $ciphertext = $this->_instance->encrypt('Something');
        $this->assertEquals($plaintext, $this->_instance->decrypt($ciphertext));
    }

    public function invalidKeyData() {
        return array(
            array(null),
            array(''),
            array('Foo'),
            array('<KEY>'),
            array('<KEY>'),
            array('<KEY>),
        );
    }

    /**
     * @dataProvider invalidKeyData
     */
    public function testInvalidEncryptionKeyThrowsException($invalidKey) {
        $this->setExpectedException('InvalidArgumentException', 'Encryption key must be');
        new Simple($invalidKey, $this->_macKey);
    }
    
    /**
     * @dataProvider invalidKeyData
     */
    public function testInvalidMacKeySizeThrowsException($invalidKey) {
        $this->setExpectedException('InvalidArgumentException', 'MAC key must be');
        new Simple($this->_encryptionKey, $invalidKey);
    }

    public function testEncryptWithInvalidIvLengthThrowsException() {
        $this->setExpectedException('InvalidArgumentException', 'IV must be exactly');

{'fraction_non_alphanumeric': 0.12742980561555076, 'fraction_numerical': 0.014038876889848811, 'mean_word_length': 2.688053097345133, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 75, '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': '861881', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2416, 'n_words': 406}