<commit_before><?php

class PasswordTest extends \Codeception\TestCase\Test
{
	public function testIsValidAlgorithm()
	{
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm('bcrypt'));
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm('whirlpool,pbkdf2'));
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm(array('md5', 'sha1', 'md5')));
		
		$this->assertFalse(Rhymix\Framework\Password::isValidAlgorithm('bunga_bunga'));
		Rhymix\Framework\Password::addAlgorithm('bunga_bunga', '/bunga_bunga/', function($hash) { return 'bunga_bunga'; });
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm('bunga_bunga'));
	}
	
	public function testGetSupportedAlgorithms()
	{
		$algos = Rhymix\Framework\Password::getSupportedAlgorithms();
		$this->assertTrue(in_array('bcrypt', $algos));
		$this->assertTrue(in_array('pbkdf2', $algos));
		$this->assertTrue(in_array('md5', $algos));
	}
	
	public function testGetBestSupportedAlgorithm()
	{
		$algo = Rhymix\Framework\Password::getBestSupportedAlgorithm();
		$this->assertTrue($algo === 'bcrypt' || $algo === 'pbkdf2');
	}
	
	public function testGetDefaultAlgorithm()
	{
		$algo = Rhymix\Framework\Password::getDefaultAlgorithm();
		$this->assertTrue($algo === 'bcrypt' || $algo === 'pbkdf2' || $algo === 'md5');
	}
	
	public function testGetWorkFactor()
	{
		$work_factor = $algo = Rhymix\Framework\Password::getWorkFactor();
		$this->assertTrue($work_factor >= 4);
		$this->assertTrue($work_factor <= 31);
	}
	
	public function testGetRandomPassword()
	{
		$password = Rhymix\Framework\Password::getRandomPassword(16);
		$this->assertEquals(16, strlen($password));
		$this->assertRegexp('/[a-z]/', $password);
		$this->assertRegexp('/[A-Z]/', $password);
		$this->assertRegexp('/[0-9]/', $password);
		$this->assertRegexp('/[^a-zA-Z0-9]/', $password);
	}
	
	public function testHashPassword()
	{
		$password = R<PASSWORD>\Framework\Security::getRandom(32);
		$this->assertEquals(md5($password), Rhymix\Framework\Password::hashPassword($password, 'md5'));
		$this->assertEquals(md5(sha1(md5($password))), Rhymix\Framework\Password::hashPassword($password, 'md5,sha1,md5'));
		$this->assertEquals(hash('wh<PASSWORD>', $password), Rhymix\Framework\Password::hashPassword($password, '<PASSWORD>'));
		$this->assertEquals('<PASSWORD>', Rhymix\Framework\Password::hashPassword('password', '<PASSWORD>'));
	}
	
	public function testCheckPassword()
	{
		$password = <PASSWORD>\Framework\Security::getRandom(32);
		
		$algos = array('whirlpool', 'ripemd160', 'bcrypt');
		$hash = Rhymix\Framework\Password::hashPassword($password, $algos);
		$this->assertRegExp('/^\$2y\$/', $hash);
		$this->assertEquals(60, strlen($hash));
		$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algos));
		
		$algos = array('pbkdf2');
		$hash = Rhymix\Framework\Password::hashPassword($password, $algos);
		$this->assertRegExp('/^(sha256|sha512):[0-9]+:/', $hash);
		$this->assertEquals(60, strlen($hash));
		$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algos));
		
		foreach (array('drupal', 'joomla', 'kimsqrb', 'mysql_old_password', 'mysql_new_password', 'mss<PASSWORD>') as $algo)
		{
			$hash = Rhymix\Framework\Password::hashPassword($password, $algo);
			$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algo));
			$this->assertFalse(Rhymix\Framework\Password::checkPassword($password, $hash . 'x', $algo));
		}
	}<commit_msg>Add more algorithms to unit test<commit_after><?php

class PasswordTest extends \Codeception\TestCase\Test
{
	public function testIsValidAlgorithm()
	{
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm('bcrypt'));
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm('whirlpool,pbkdf2'));
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm(array('md5', 'sha1', 'md5')));
		
		$this->assertFalse(Rhymix\Framework\Password::isValidAlgorithm('bunga_bunga'));
		Rhymix\Framework\Password::addAlgorithm('bunga_bunga', '/bunga_bunga/', function($hash) { return 'bunga_bunga'; });
		$this->assertTrue(Rhymix\Framework\Password::isValidAlgorithm('bunga_bunga'));
	}
	
	public function testGetSupportedAlgorithms()
	{
		$algos = Rhymix\Framework\Password::getSupportedAlgorithms();
		$this->assertTrue(in_array('bcrypt', $algos));
		$this->assertTrue(in_array('pbkdf2', $algos));
		$this->assertTrue(in_array('md5', $algos));
	}
	
	public function testGetBestSupportedAlgorithm()
	{
		$algo = Rhymix\Framework\Password::getBestSupportedAlgorithm();
		$this->assertTrue($algo === 'bcrypt' || $algo === 'pbkdf2');
	}
	
	public function testGetDefaultAlgorithm()
	{
		$algo = Rhymix\Framework\Password::getDefaultAlgorithm();
		$this->assertTrue($algo === 'bcrypt' || $algo === 'pbkdf2' || $algo === 'md5');
	}
	
	public function testGetWorkFactor()
	{
		$work_factor = $algo = Rhymix\Framework\Password::getWorkFactor();
		$this->assertTrue($work_factor >= 4);
		$this->assertTrue($work_factor <= 31);
	}
	
	public function testGetRandomPassword()
	{
		$password = Rhymix\Framework\Password::getRandomPassword(16);
		$this->assertEquals(16, strlen($password));
		$this->assertRegexp('/[a-z]/', $password);
		$this->assertRegexp('/[A-Z]/', $password);
		$this->assertRegexp('/[0-9]/', $password);
		$this->assertRegexp('/[^a-zA-Z0-9]/', $password);
	}
	
	public function testHashPassword()
	{
		$password = Rhymix\Framework\Security::getRandom(32);
		$this->assertEquals(md5($password), Rhymix\Framework\Password::hashPassword($password, 'md5'));
		$this->assertEquals(md5(sha1(md5($password))), Rhymix\Framework\Password::hashPassword($password, 'md5,sha1,md5'));
		$this->assertEquals(hash('whirlpool', $password), Rhymix\Framework\Password::hashPassword($password, '<PASSWORD>'));
		$this->assertEquals('<PASSWORD>', Rhymix\Framework\Password::hashPassword('password', '<PASSWORD>'));
	}
	
	public function testCheckPassword()
	{
		$password = Rhymix\Framework\Security::getRandom(32);
		
		$algos = array('whirlpool', 'ripemd160', 'bcrypt');
		$hash = Rhymix\Framework\Password::hashPassword($password, $algos);
		$this->assertRegExp('/^\$2y\$/', $hash);
		$this->assertEquals(60, strlen($hash));
		$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algos));
		
		$algos = array('sha384', 'pbkdf2');
		$hash = Rhymix\Framework\Password::hashPassword($password, $algos);
		$this->assertRegExp('/^(sha256|sha512):[0-9]+:/', $hash);
		$this->assertEquals(60, strlen($hash));
		$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algos));
		
		$algos = array('sha1', 'portable');
		$hash = Rhymix\Framework\Password::hashPassword($password, $algos);
		$this->assertRegExp('/^\$P\$/', $hash);
		$this->assertEquals(34, strlen($hash));
		$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algos));
		
		foreach (array('drupal', 'joomla', 'kimsqrb', 'mysql_old_password', 'mysql_new_password', '<PASSWORD>') as $algo)
		{
			$hash = Rhymix\Framework\Password::hashPassword($password, $algo);
			$this->assertTrue(Rhymix\Framework\Password::checkPassword($password, $hash, $algo));
			$this->assertFalse(Rhymix\Framework\Password::checkPassword($password, $hash . 'x', $algo));
		}
	}
{'fraction_non_alphanumeric': 0.20002749518834204, 'fraction_numerical': 0.014297497937860875, 'mean_word_length': 6.92483660130719, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 77, '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': '2633482', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2464, 'n_words': 307}