1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace PHPassLib\Test\Hash;
13:
14: use PHPassLib\Hash\BCrypt;
15:
16: 17: 18: 19: 20: 21: 22: 23:
24: class BCryptTest extends \PHPUnit_Framework_TestCase
25: {
26:
27: 28: 29: 30: 31:
32: public function validTestVectorProvider()
33: {
34: $vectors = array (
35:
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: 56: 57: 58:
59: public function invalidTestVectorProvider()
60: {
61: $vectors = array (
62:
63: array ("", '$2a$03$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
64: array ("", '$2a$32$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
65: array ("", '$2z$05$CCCCCCCCCCCCCCCCCCCCC.', '*0'),
66:
67:
68:
69:
70:
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: 82: 83: 84: 85: 86: 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: 97: 98: 99: 100: 101: 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: 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: