<commit_before><?php
/**************************************************************************
Copyright 2017 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 *****************************************************************************/

namespace Gishiki\tests\Security\Hashing;

use PHPUnit\Framework\TestCase;

use Gishiki\Security\Hashing\HashingException;
use Gishiki\Security\Hashing\Algorithm;

/**
 * Tests for the Hasher class.
 *
 * @author <NAME> <<EMAIL>>
 */
class AlgorithmTest extends TestCase
{
    public function testInvalidMessageForOpensslHash()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslHash('', Algorithm::SHA512);
    }

    public function testInvalidMessageForRot13Hash()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::rot13Hash('');
    }

    public function testInvalidMessageForBcryptHash()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::bryptHash('');
    }

    public function testInvalidMessageForOpensslVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslVerify('', ':)', Algorithm::SHA512);
    }

    public function testInvalidMessageForRot13Verify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::rot13Verify('', ':)');
    }

    public function testInvalidMessageForBcryptVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::bcryptVerify('', ':)');
    }

    public function testInvalidMessageDigestForOpensslVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslVerify('My message', '', Algorithm::SHA512);
    }

    public function testInvalidMessageDigestForRot13Verify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::rot13Verify('My message', '');
    }

    public function testInvalidMessageDigestForBcryptVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::bcryptVerify('My message', '');
    }

    public function testOpensslVerify()
    {
        $random = openssl_random_pseudo_bytes(25);

        //test hash compatibility
        $hash = Algorithm::opensslHash($random, Algorithm::SHA512);
        $this->assertEquals(true, Algorithm::opensslVerify($random, $hash, Algorithm::SHA512));

        $this->assertEquals(false, Algorithm::opensslVerify($random, 'any other thing', Algorithm::SHA512));
    }

    public function testRot13Verify()
    {
        $random = openssl_random_pseudo_bytes(25);

        //test hash compatibility
        $hash = Algorithm::opensslHash($random);
        $this->assertEquals(true, Algorithm::rot13Hash($random, $hash));

        $this->assertEquals(false, Algorithm::rot13Verify($random, 'any other thing'));
    }

    public function testBCryptVerify()
    {
        $random = openssl_random_pseudo_bytes(25);

        //test hash compatibility
        $hash = Algorithm::opensslHash($random);
        $this->assertEquals(true, Algorithm::bcryptHash($random, $hash));

        $this->assertEquals(false, Algorithm::bcryptVerify($random, 'any other thing'));
    }

    public function testInvalidAlgorithm()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslHash('my message', null);
    }

    public function testROT13()
    {
        $message = 'this is a small>example<to/test rot-13';
        $message_rot13 = 'guvf vf n fznyy>rknzcyr<gb/grfg ebg-13';

        //test hash compatibility
        $rot_ed = Algorithm::rot13Hash($message, Algorithm::ROT13);
        $this->assertEquals($message_rot13, $rot_ed);
        $this->assertEquals($message, Algorithm::hash($rot_ed, Algorithm::ROT13));

        $this->assertEquals(true, Algorithm::rot13Verify($message, $rot_ed));
    }

    public function testHashCompatibility()
    {
        $message = openssl_random_pseudo_bytes(128);

        //test hash compatibility
        $this->assertEquals(md5($message), Algorithm::opensslHash($message, Algorithm::MD5));
        $this->assertEquals(sha1($message), Algorithm::opensslHash($message, Algorithm::SHA1));
    }

    public function testBadHashPbkdf2()
    {
        $this->expectException(HashingException::class);
        
        Algorithm::pbkdf2('password', 'salt', 512, 3, 'bad-algo');
    }

    public function testBadAlgorithmPbkdf2()<commit_msg>Fixing tests<commit_after><?php
/**************************************************************************
Copyright 2017 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 *****************************************************************************/

namespace Gishiki\tests\Security\Hashing;

use PHPUnit\Framework\TestCase;

use Gishiki\Security\Hashing\HashingException;
use Gishiki\Security\Hashing\Algorithm;

/**
 * Tests for the Hasher class.
 *
 * @author <NAME> <<EMAIL>>
 */
class AlgorithmTest extends TestCase
{
    public function testInvalidMessageForOpensslHash()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslHash('', Algorithm::SHA512);
    }

    public function testInvalidMessageForRot13Hash()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::rot13Hash('');
    }

    public function testInvalidMessageForBcryptHash()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::bryptHash('');
    }

    public function testInvalidMessageForOpensslVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslVerify('', ':)', Algorithm::SHA512);
    }

    public function testInvalidMessageForRot13Verify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::rot13Verify('', ':)');
    }

    public function testInvalidMessageForBcryptVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::bcryptVerify('', ':)');
    }

    public function testInvalidMessageDigestForOpensslVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::opensslVerify('My message', '', Algorithm::SHA512);
    }

    public function testInvalidMessageDigestForRot13Verify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::rot13Verify('My message', '');
    }

    public function testInvalidMessageDigestForBcryptVerify()
    {
        $this->expectException(\InvalidArgumentException::class);

        //test hash compatibility
        Algorithm::bcryptVerify('My message', '');
    }

    public function testOpensslVerify()
    {
        $random = openssl_random_pseudo_bytes(25);

        //test hash compatibility
        $hash = Algorithm::opensslHash($random, Algorithm::SHA512);
        $this->assertEquals(true, Algorithm::opensslVerify($random, $hash, Algorithm::SHA512));

        $this->assertEquals(false, Algorithm::opensslVerify($random, 'any other thing', Algorithm::SHA512));
    }

    public function testRot13Verify()
    {
        $random = openssl_random_pseudo_bytes(25);

        //test hash compatibility
        $hash = Algorithm::rot13Hash($random);
        $this->assertEquals(true, Algorithm::rot13Hash($random, $hash));

        $this->assertEquals(false, Algorithm::rot13Verify($random, 'any other thing'));
    }

    public function testBCryptVerify()
    {
        $random = openssl_random_pseudo_bytes(25);

        //test hash compatibility
        $hash = Algorithm::bcryptHash($random);
        $this->assertEquals(true, Algorithm::bcryptHash($random, $hash));

        $this->assertEquals(false, Algorithm::bcryptVerify($random, 'any other thing'));
    }

    public function testROT13()
    {
        $message = 'this is a small>example<to/test rot-13';
        $message_rot13 = 'guvf vf n fznyy>rknzcyr<gb/grfg ebg-13';

        //test hash compatibility
        $rot_ed = Algorithm::rot13Hash($message, Algorithm::ROT13);
        $this->assertEquals($message_rot13, $rot_ed);
        $this->assertEquals($message, Algorithm::rot13Hash($rot_ed, Algorithm::ROT13));

        $this->assertEquals(true, Algorithm::rot13Verify($message, $rot_ed));
    }

    public function testHashCompatibility()
    {
        $message = openssl_random_pseudo_bytes(128);

        //test hash compatibility
        $this->assertEquals(md5($message), Algorithm::opensslHash($message, Algorithm::MD5));
        $this->assertEquals(sha1($message), Algorithm::opensslHash($message, Algorithm::SHA1));
    }

    public function testBadHashPbkdf2()
    {
        $this->expectException(HashingException::class);
        
        Algorithm::pbkdf2('password', 'salt', 512, 3, 'bad-algo');
    }

    public function testBadAlgorithmPbkdf2()
{'fraction_non_alphanumeric': 0.14482431149097816, 'fraction_numerical': 0.015954415954415956, 'mean_word_length': 3.61077057793345, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6633767', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2669, 'n_words': 594}