1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2015 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PBKDF2\tests; |
13
|
|
|
|
14
|
|
|
use PBKDF2\PBKDF2; |
15
|
|
|
|
16
|
|
|
class CustomTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
public function testVector0() |
19
|
|
|
{ |
20
|
|
|
$algorithm = 'md5'; |
21
|
|
|
$password = 'password'; |
22
|
|
|
$salt = 'salt'; |
23
|
|
|
$count = 1; |
24
|
|
|
$hex_expected = 'f31afb6d931392daa5e3130f47f9a9b6e8e72029d8350b9fb27a9e0e00b9d9915a5f18928639ca8bbc3d1c1cb66d4f27b9df'; |
25
|
|
|
$raw_expected = pack('H*', $hex_expected); |
26
|
|
|
|
27
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 50, true)); |
28
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 50, true, false)); |
29
|
|
|
$this->assertSame($hex_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 50, false, false)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testVector1() |
33
|
|
|
{ |
34
|
|
|
$algorithm = 'sha256'; |
35
|
|
|
$password = 'password'; |
36
|
|
|
$salt = 'salt'; |
37
|
|
|
$count = 1; |
38
|
|
|
$hex_expected = '120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b'; |
39
|
|
|
$raw_expected = pack('H*', $hex_expected); |
40
|
|
|
|
41
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true)); |
42
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true, false)); |
43
|
|
|
$this->assertSame($hex_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, false, false)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testVector2() |
47
|
|
|
{ |
48
|
|
|
$algorithm = 'sha512'; |
49
|
|
|
$password = 'password'; |
50
|
|
|
$salt = 'salt'; |
51
|
|
|
$count = 2; |
52
|
|
|
$hex_expected = 'e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e'; |
53
|
|
|
$raw_expected = pack('H*', $hex_expected); |
54
|
|
|
|
55
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true)); |
56
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true, false)); |
57
|
|
|
$this->assertSame($hex_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, false, false)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testVector3() |
61
|
|
|
{ |
62
|
|
|
$algorithm = 'md2'; |
63
|
|
|
$password = 'password'; |
64
|
|
|
$salt = 'salt'; |
65
|
|
|
$count = 4096; |
66
|
|
|
$hex_expected = '71f64353e1c9af0564da05d554aeafe7'; |
67
|
|
|
$raw_expected = pack('H*', $hex_expected); |
68
|
|
|
|
69
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true)); |
70
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true, false)); |
71
|
|
|
$this->assertSame($hex_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, false, false)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testVector4() |
75
|
|
|
{ |
76
|
|
|
$algorithm = 'whirlpool'; |
77
|
|
|
$password = 'password'; |
78
|
|
|
$salt = 'salt'; |
79
|
|
|
$count = 1000; |
80
|
|
|
$hex_expected = '5ad7361484c7dde6b23e573c4b61d1fd16023fd6c0170d0b26d70f7ac8c683f06e767804750357b4032297c2ad36cbd84d01476c1298826b71f605dbcda9e055'; |
81
|
|
|
$raw_expected = pack('H*', $hex_expected); |
82
|
|
|
|
83
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true)); |
84
|
|
|
$this->assertSame($raw_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, true, false)); |
85
|
|
|
$this->assertSame($hex_expected, PBKDF2::deriveKey($algorithm, $password, $salt, $count, 0, false, false)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @expectedException InvalidArgumentException |
90
|
|
|
* @expectedExceptionMessage PBKDF2 ERROR: Invalid key length parameters. |
91
|
|
|
*/ |
92
|
|
|
public function testVector5() |
93
|
|
|
{ |
94
|
|
|
$algorithm = 'whirlpool'; |
95
|
|
|
$password = 'password'; |
96
|
|
|
$salt = 'salt'; |
97
|
|
|
$count = 1000; |
98
|
|
|
|
99
|
|
|
PBKDF2::deriveKey($algorithm, $password, $salt, $count, -1, true, false); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @expectedException InvalidArgumentException |
104
|
|
|
* @expectedExceptionMessage PBKDF2 ERROR: Invalid hash algorithm. |
105
|
|
|
*/ |
106
|
|
|
public function testVector6() |
107
|
|
|
{ |
108
|
|
|
$algorithm = 'foo'; |
109
|
|
|
$password = 'password'; |
110
|
|
|
$salt = 'salt'; |
111
|
|
|
$count = 1000; |
112
|
|
|
|
113
|
|
|
PBKDF2::deriveKey($algorithm, $password, $salt, $count, -1, true, false); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|