1
+ <?php
2
+ /**
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ namespace doganoo \PHPUtil \Datatype ;
27
+
28
+ /**
29
+ * Class PasswordClass
30
+ * @package doganoo\PHPUtil\Datatype
31
+ */
32
+ class PasswordClass extends StringClass {
33
+ /** @var string PASSWORD_BCRYPT_NAME */
34
+ public const PASSWORD_BCRYPT_NAME = "PASSWORD_BCRYPT " ;
35
+ /** @var int $algorithm */
36
+ private $ algorithm = PASSWORD_BCRYPT ;
37
+
38
+ /**
39
+ * PasswordClass constructor.
40
+ * @param $value
41
+ */
42
+ public function __construct ($ value ) {
43
+ parent ::__construct ($ value );
44
+ $ this ->hash ();
45
+ }
46
+
47
+ /**
48
+ * hashes the string
49
+ */
50
+ public function hash (): void {
51
+ parent ::setValue (password_hash ($ this ->getValue (), $ this ->getAlgorithm ()));
52
+ }
53
+
54
+ /**
55
+ * verifies the password
56
+ *
57
+ * @param $password
58
+ * @return bool
59
+ */
60
+ public function verify ($ password ): bool {
61
+ if ($ password instanceof StringClass || $ password instanceof PasswordClass) {
62
+ return password_verify ($ password ->getValue (), $ this ->getValue ());
63
+ }
64
+ return password_verify ($ password , $ this ->getValue ());
65
+ }
66
+
67
+ /**
68
+ * returns the algorithm
69
+ * @return int
70
+ */
71
+ public function getAlgorithm (): int {
72
+ return $ this ->algorithm ;
73
+ }
74
+
75
+ /**
76
+ * returns the algorithm name
77
+ *
78
+ * @return StringClass|null
79
+ */
80
+ public function getAlgorithmName ():?StringClass {
81
+ if ($ this ->getAlgorithm () === PASSWORD_BCRYPT ){
82
+ return new StringClass (PasswordClass::PASSWORD_BCRYPT_NAME );
83
+ }
84
+ return null ;
85
+ }
86
+
87
+ /**
88
+ * sets the algorithm
89
+ *
90
+ * @param int $algorithm
91
+ */
92
+ public function setAlgorithm (int $ algorithm ): void {
93
+ $ this ->algorithm = $ algorithm ;
94
+ }
95
+
96
+
97
+ }
0 commit comments