Overview

Namespaces

  • PHP
  • PHPassLib
    • Application
    • Exception
    • Hash
    • Test
      • Application
      • Hash

Classes

  • BCryptTest
  • BSDiCryptTest
  • DESCryptTest
  • MD5CryptTest
  • PBKDF2Test
  • PortableTest
  • SHA1CryptTest
  • SHA256CryptTest
  • SHA512CryptTest
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * PHP Password Library
  4:  *
  5:  * @package PHPassLib\Tests
  6:  * @author Ryan Chouinard <rchouinard@gmail.com>
  7:  * @copyright Copyright (c) 2012, Ryan Chouinard
  8:  * @license MIT License - http://www.opensource.org/licenses/mit-license.php
  9:  * @version 3.0.0-dev
 10:  */
 11: 
 12: namespace PHPassLib\Test\Hash;
 13: 
 14: use PHPassLib\Hash\PBKDF2;
 15: 
 16: /**
 17:  * PBKDF2-<digest> Module Tests
 18:  *
 19:  * @package PHPassLib\Tests
 20:  * @author Ryan Chouinard <rchouinard@gmail.com>
 21:  * @copyright Copyright (c) 2012, Ryan Chouinard
 22:  * @license MIT License - http://www.opensource.org/licenses/mit-license.php
 23:  */
 24: class PBKDF2Test extends \PHPUnit_Framework_TestCase
 25: {
 26: 
 27:     /**
 28:      * Provide valid test vectors.
 29:      *
 30:      * @return array
 31:      */
 32:     public function validTestVectorProvider()
 33:     {
 34:         $vectors = array (
 35:             // Generated using the Python PassLib
 36:             array ("password", '$pbkdf2$1212$OB.dtnSEXZK8U5cgxU/GYQ$y5LKPOplRmok7CZp/aqVDVg8zGI'),
 37:             array ("password", '$pbkdf2-sha256$1212$4vjV83LKPjQzk31VI4E0Vw$hsYF68OiOUPdDZ1Fg.fJPeq1h/gXXY7acBp9/6c.tmQ'),
 38:             array ("password", '$pbkdf2-sha512$1212$RHY0Fr3IDMSVO/RSZyb5ow$eNLfBK.eVozomMr.1gYa17k9B7KIK25NOEshvhrSX.esqY3s.FvWZViXz4KoLlQI.BzY/YTNJOiKc5gBYFYGww'),
 39:         );
 40: 
 41:         return $vectors;
 42:     }
 43: 
 44:     /**
 45:      * Provide invalid test vectors.
 46:      *
 47:      * @return array
 48:      */
 49:     public function invalidTestVectorProvider()
 50:     {
 51:         $vectors = array (
 52:             array ("", '$pbkdf2$01212$THDqatpidANpadlLeTeOEg$HV3oi1k5C5LQCgG1BMOL.BX4YZc', '*0'),
 53:             array ("", '*0', '*1'),
 54:             array ("", '*1', '*0'),
 55:         );
 56: 
 57:         return $vectors;
 58:     }
 59: 
 60:     /**
 61:      * Verify that the class produces correct results with valid test vectors.
 62:      *
 63:      * @test
 64:      * @dataProvider validTestVectorProvider
 65:      * @param string $password
 66:      * @param string $hash
 67:      */
 68:     public function validTestVectorsProduceExpectedResults($password, $hash)
 69:     {
 70:         $this->assertEquals($hash, PBKDF2::hash($password, $hash));
 71:         $this->assertTrue(PBKDF2::verify($password, $hash));
 72:     }
 73: 
 74:     /**
 75:      * Verify that the class produces correct results with invalid test vectors.
 76:      *
 77:      * @test
 78:      * @dataProvider invalidTestVectorProvider
 79:      * @param string $password
 80:      * @param string $hash
 81:      */
 82:     public function invalidTestVectorsProduceExpectedResults($password, $hash, $errorString)
 83:     {
 84:         $this->assertEquals($errorString, PBKDF2::hash($password, $hash));
 85:         $this->assertFalse(PBKDF2::verify($password, $hash));
 86:     }
 87: 
 88:     /**
 89:      * @test
 90:      */
 91:     public function genconfigAndParseconfigProduceMatchingResults()
 92:     {
 93:         $options = array (
 94:             'digest' => 'sha256',
 95:             'rounds' => '1212',
 96:             'salt' => '4vjV83LKPjQzk31VI4E0Vw.',
 97:         );
 98:         $config = PBKDF2::genConfig($options);
 99:         $options['saltSize'] = 17;
100: 
101:         $this->assertEquals('$pbkdf2-sha256$1212$4vjV83LKPjQzk31VI4E0Vw.', $config);
102:         $this->assertSame($options, PBKDF2::parseConfig($config));
103:     }
104: 
105: }
106: 
PHP Password Library API documentation generated by ApiGen 2.8.0