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\BCrypt;
 15: 
 16: /**
 17:  * BCrypt 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 BCryptTest 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", '$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW'),
 37:             array ("U*U*", '$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK'),
 38:             array ("U*U*U", '$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a'),
 39:             array ("", '$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy'),
 40:             array ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789chars after 72 are ignored", '$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui'),
 41:             array ("\xa3", '$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e'),
 42:             array ("\xa3", '$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq'),
 43:             array ("\xd1\x91", '$2x$05$6bNw2HLQYeqHYyBfLMsv/OiwqTymGIGzFsA4hOTWebfehXHNprcAS'),
 44:             array ("\xd0\xc1\xd2\xcf\xcc\xd8", '$2x$05$6bNw2HLQYeqHYyBfLMsv/O9LIGgn8OMzuDoHfof8AQimSGfcSWxnS'),
 45:             array ("\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaachars after 72 are ignored as usual", '$2a$05$/OK.fbVrR/bpIqNJ5ianF.swQOIzjOiJ9GHEPuhEkvqrUyvWhEMx6'),
 46:             array ("\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55", '$2a$05$/OK.fbVrR/bpIqNJ5ianF.R9xrDjiycxMbQE2bp.vgqlYpW5wx2yy'),
 47:             array ("", '$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy'),
 48:             array ("\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff", '$2a$05$/OK.fbVrR/bpIqNJ5ianF.9tQZzcJfm3uj2NvJ/n5xkhpqLrMpWCe'),
 49:         );
 50: 
 51:         return $vectors;
 52:     }
 53: 
 54:     /**
 55:      * Provide invalid test vectors.
 56:      *
 57:      * @return array
 58:      */
 59:     public function invalidTestVectorProvider()
 60:     {
 61:         $vectors = array (
 62:             // From Openwall's crypt v1.2
 63:             array ("", '$2a$03$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
 64:             array ("", '$2a$32$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
 65:             array ("", '$2z$05$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
 66: 
 67:             // PHP's crypt actually fails the following tests, so the adapter
 68:             // works around them.
 69:             //
 70:             // See https://bugs.php.net/bug.php?id=61852
 71:             array ("", '$2`$05$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
 72:             array ("", '$2{$05$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
 73:             array ("", '*0', '*1'),
 74:             array ("", '*1', '*0'),
 75:         );
 76: 
 77:         return $vectors;
 78:     }
 79: 
 80:     /**
 81:      * Verify that the class produces correct results with valid test vectors.
 82:      *
 83:      * @test
 84:      * @dataProvider validTestVectorProvider
 85:      * @param string $password
 86:      * @param string $hash
 87:      */
 88:     public function validTestVectorsProduceExpectedResults($password, $hash)
 89:     {
 90:         $config = substr($hash, 0, 29);
 91:         $this->assertEquals($hash, BCrypt::hash($password, $config));
 92:         $this->assertTrue(BCrypt::verify($password, $hash));
 93:     }
 94: 
 95:     /**
 96:      * Verify that the class produces correct results with invalid test vectors.
 97:      *
 98:      * @test
 99:      * @dataProvider invalidTestVectorProvider
100:      * @param string $password
101:      * @param string $hash
102:      */
103:     public function invalidTestVectorsProduceExpectedResults($password, $hash, $errorString)
104:     {
105:         $config = substr($hash, 0, 29);
106:         $this->assertEquals($errorString, BCrypt::hash($password, $config));
107:         $this->assertFalse(BCrypt::verify($password, $hash));
108:     }
109: 
110:     /**
111:      * @test
112:      */
113:     public function genconfigAndParseconfigProduceMatchingResults()
114:     {
115:         $options = array (
116:             'ident' => '2x',
117:             'rounds' => 8,
118:             'salt' => 'CCCCCCCCCCCCCCCCCCCCC.',
119:         );
120:         $config = BCrypt::genConfig($options);
121: 
122:         $this->assertEquals('$2x$08$CCCCCCCCCCCCCCCCCCCCC.', $config);
123:         $this->assertSame($options, BCrypt::parseConfig($config));
124:     }
125: 
126: }
127: 
PHP Password Library API documentation generated by ApiGen 2.8.0