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\BSDiCrypt;
 15: 
 16: /**
 17:  * BSDi / Extended DES Crypt 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 BSDiCryptTest 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:             // From John the Ripper 1.7.9
 36:             array ("U*U*U*U*", '_J9..CCCCXBrJUJV154M'),
 37:             array ("U*U***U", '_J9..CCCCXUhOBTXzaiE'),
 38:             array ("U*U***U*", '_J9..CCCC4gQ.mB/PffM'),
 39:             array ("*U*U*U*U", '_J9..XXXXvlzQGqpPPdk'),
 40:             array ("*U*U*U*U*", '_J9..XXXXsqM/YSSP..Y'),
 41:             array ("*U*U*U*U*U*U*U*U", '_J9..XXXXVL7qJCnku0I'),
 42:             array ("*U*U*U*U*U*U*U*U*", '_J9..XXXXAj8cFbP5scI'),
 43:             array ("ab1234567", '_J9..SDizh.vll5VED9g'),
 44:             array ("cr1234567", '_J9..SDizRjWQ/zePPHc'),
 45:             array ("zxyDPWgydbQjgq", '_J9..SDizxmRI1GjnQuE'),
 46:             array ("726 even", '_K9..SaltNrQgIYUAeoY'),
 47:             array ("", '_J9..SDSD5YGyRCr4W4c'),
 48:         );
 49: 
 50:         return $vectors;
 51:     }
 52: 
 53:     /**
 54:      * Provide invalid test vectors.
 55:      *
 56:      * @return array
 57:      */
 58:     public function invalidTestVectorProvider()
 59:     {
 60:         $vectors = array (
 61:             array ("", '_K1.!crsmZxOLzfJH8iw', '*0'),
 62:             array ("", '*0', '*1'),
 63:             array ("", '*1', '*0'),
 64:         );
 65: 
 66:         return $vectors;
 67:     }
 68: 
 69:     /**
 70:      * Verify that the class produces correct results with valid test vectors.
 71:      *
 72:      * @test
 73:      * @dataProvider validTestVectorProvider
 74:      * @param string $password
 75:      * @param string $hash
 76:      */
 77:     public function validTestVectorsProduceExpectedResults($password, $hash)
 78:     {
 79:         $config = substr($hash, 0, 9);
 80:         $this->assertEquals($hash, BSDiCrypt::hash($password, $config));
 81:         $this->assertTrue(BSDiCrypt::verify($password, $hash));
 82:     }
 83: 
 84:     /**
 85:      * Verify that the class produces correct results with invalid test vectors.
 86:      *
 87:      * @test
 88:      * @dataProvider invalidTestVectorProvider
 89:      * @param string $password
 90:      * @param string $hash
 91:      */
 92:     public function invalidTestVectorsProduceExpectedResults($password, $hash, $errorString)
 93:     {
 94:         $config = substr($hash, 0, 9);
 95:         $this->assertEquals($errorString, BSDiCrypt::hash($password, $config));
 96:         $this->assertFalse(BSDiCrypt::verify($password, $hash));
 97:     }
 98: 
 99:     /**
100:      * @test
101:      */
102:     public function genconfigAndParseconfigProduceMatchingResults()
103:     {
104:         $options = array (
105:             'rounds' => 5001,
106:             'salt' => 'CCC.',
107:         );
108:         $config = BSDiCrypt::genConfig($options);
109: 
110:         $this->assertEquals('_7C/.CCC.', $config);
111:         $this->assertSame($options, BSDiCrypt::parseConfig($config));
112: 
113:         $options = array (
114:             'rounds' => 5000,
115:             'salt' => 'CCC.',
116:         );
117:         $config = BSDiCrypt::genConfig($options);
118:         $options['rounds'] = 4999; // Module subtracts 1 from even rounds
119:                                    // when generating the config string.
120: 
121:         $this->assertEquals('_5C/.CCC.', $config, $config);
122:         $this->assertSame($options, BSDiCrypt::parseConfig($config));
123:     }
124: 
125: }
126: 
PHP Password Library API documentation generated by ApiGen 2.8.0