55use InvalidArgumentException ;
66use LdapRecord \LdapRecordException ;
77use ReflectionMethod ;
8+ use SensitiveParameter ;
89
910class Password
1011{
@@ -17,87 +18,87 @@ class Password
1718 /**
1819 * Make an encoded password for transmission over LDAP.
1920 */
20- public static function encode (string $ password ): string
21+ public static function encode (#[SensitiveParameter] string $ password ): string
2122 {
2223 return iconv ('UTF-8 ' , 'UTF-16LE ' , '" ' .$ password .'" ' );
2324 }
2425
2526 /**
2627 * Make a salted md5 password.
2728 */
28- public static function smd5 (string $ password , ?string $ salt = null ): string
29+ public static function smd5 (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
2930 {
3031 return '{SMD5} ' .static ::makeHash ($ password , 'md5 ' , null , $ salt ?? random_bytes (4 ));
3132 }
3233
3334 /**
3435 * Make a salted SHA password.
3536 */
36- public static function ssha (string $ password , ?string $ salt = null ): string
37+ public static function ssha (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
3738 {
3839 return '{SSHA} ' .static ::makeHash ($ password , 'sha1 ' , null , $ salt ?? random_bytes (4 ));
3940 }
4041
4142 /**
4243 * Make a salted SSHA256 password.
4344 */
44- public static function ssha256 (string $ password , ?string $ salt = null ): string
45+ public static function ssha256 (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
4546 {
4647 return '{SSHA256} ' .static ::makeHash ($ password , 'hash ' , 'sha256 ' , $ salt ?? random_bytes (4 ));
4748 }
4849
4950 /**
5051 * Make a salted SSHA384 password.
5152 */
52- public static function ssha384 (string $ password , ?string $ salt = null ): string
53+ public static function ssha384 (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
5354 {
5455 return '{SSHA384} ' .static ::makeHash ($ password , 'hash ' , 'sha384 ' , $ salt ?? random_bytes (4 ));
5556 }
5657
5758 /**
5859 * Make a salted SSHA512 password.
5960 */
60- public static function ssha512 (string $ password , ?string $ salt = null ): string
61+ public static function ssha512 (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
6162 {
6263 return '{SSHA512} ' .static ::makeHash ($ password , 'hash ' , 'sha512 ' , $ salt ?? random_bytes (4 ));
6364 }
6465
6566 /**
6667 * Make a non-salted SHA password.
6768 */
68- public static function sha (string $ password ): string
69+ public static function sha (#[SensitiveParameter] string $ password ): string
6970 {
7071 return '{SHA} ' .static ::makeHash ($ password , 'sha1 ' );
7172 }
7273
7374 /**
7475 * Make a non-salted SHA256 password.
7576 */
76- public static function sha256 (string $ password ): string
77+ public static function sha256 (#[SensitiveParameter] string $ password ): string
7778 {
7879 return '{SHA256} ' .static ::makeHash ($ password , 'hash ' , 'sha256 ' );
7980 }
8081
8182 /**
8283 * Make a non-salted SHA384 password.
8384 */
84- public static function sha384 (string $ password ): string
85+ public static function sha384 (#[SensitiveParameter] string $ password ): string
8586 {
8687 return '{SHA384} ' .static ::makeHash ($ password , 'hash ' , 'sha384 ' );
8788 }
8889
8990 /**
9091 * Make a non-salted SHA512 password.
9192 */
92- public static function sha512 (string $ password ): string
93+ public static function sha512 (#[SensitiveParameter] string $ password ): string
9394 {
9495 return '{SHA512} ' .static ::makeHash ($ password , 'hash ' , 'sha512 ' );
9596 }
9697
9798 /**
9899 * Make a non-salted md5 password.
99100 */
100- public static function md5 (string $ password ): string
101+ public static function md5 (#[SensitiveParameter] string $ password ): string
101102 {
102103 return '{MD5} ' .static ::makeHash ($ password , 'md5 ' );
103104 }
@@ -110,7 +111,7 @@ public static function md5(string $password): string
110111 *
111112 * @throws LdapRecordException
112113 */
113- public static function argon2i (string $ password ): string
114+ public static function argon2i (#[SensitiveParameter] string $ password ): string
114115 {
115116 if (! defined ('PASSWORD_ARGON2I ' )) {
116117 throw new LdapRecordException ('Argon2i hashing is not supported by this PHP build. ' );
@@ -127,7 +128,7 @@ public static function argon2i(string $password): string
127128 *
128129 * @throws LdapRecordException
129130 */
130- public static function argon2id (string $ password ): string
131+ public static function argon2id (#[SensitiveParameter] string $ password ): string
131132 {
132133 if (! defined ('PASSWORD_ARGON2ID ' )) {
133134 throw new LdapRecordException ('Argon2id hashing is not supported by this PHP build. ' );
@@ -139,39 +140,39 @@ public static function argon2id(string $password): string
139140 /**
140141 * Make a non-salted NThash password.
141142 */
142- public static function nthash (string $ password ): string
143+ public static function nthash (#[SensitiveParameter] string $ password ): string
143144 {
144145 return '{NTHASH} ' .strtoupper (hash ('md4 ' , iconv ('UTF-8 ' , 'UTF-16LE ' , $ password )));
145146 }
146147
147148 /**
148149 * Crypt password with an MD5 salt.
149150 */
150- public static function md5Crypt (string $ password , ?string $ salt = null ): string
151+ public static function md5Crypt (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
151152 {
152153 return '{CRYPT} ' .static ::makeCrypt ($ password , static ::CRYPT_SALT_TYPE_MD5 , $ salt );
153154 }
154155
155156 /**
156157 * Crypt password with a SHA256 salt.
157158 */
158- public static function sha256Crypt (string $ password , ?string $ salt = null ): string
159+ public static function sha256Crypt (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
159160 {
160161 return '{CRYPT} ' .static ::makeCrypt ($ password , static ::CRYPT_SALT_TYPE_SHA256 , $ salt );
161162 }
162163
163164 /**
164165 * Crypt a password with a SHA512 salt.
165166 */
166- public static function sha512Crypt (string $ password , ?string $ salt = null ): string
167+ public static function sha512Crypt (#[SensitiveParameter] string $ password , ?string $ salt = null ): string
167168 {
168169 return '{CRYPT} ' .static ::makeCrypt ($ password , static ::CRYPT_SALT_TYPE_SHA512 , $ salt );
169170 }
170171
171172 /**
172173 * Make a new password hash.
173174 */
174- protected static function makeHash (string $ password , string $ method , ?string $ algo = null , ?string $ salt = null ): string
175+ protected static function makeHash (#[SensitiveParameter] string $ password , string $ method , ?string $ algo = null , ?string $ salt = null ): string
175176 {
176177 $ params = $ algo ? [$ algo , $ password .$ salt ] : [$ password .$ salt ];
177178
@@ -181,7 +182,7 @@ protected static function makeHash(string $password, string $method, ?string $al
181182 /**
182183 * Make a hashed password.
183184 */
184- protected static function makeCrypt (string $ password , int $ type , ?string $ salt = null ): string
185+ protected static function makeCrypt (#[SensitiveParameter] string $ password , int $ type , ?string $ salt = null ): string
185186 {
186187 return crypt ($ password , $ salt ?? static ::makeCryptSalt ($ type ));
187188 }
@@ -221,7 +222,7 @@ protected static function makeCryptPrefixAndLength(int $type): array
221222 /**
222223 * Attempt to retrieve the hash method used for the password.
223224 */
224- public static function getHashMethod (string $ password ): ?string
225+ public static function getHashMethod (#[SensitiveParameter] string $ password ): ?string
225226 {
226227 if (! preg_match ('/^\{(\w+)\}/ ' , $ password , $ matches )) {
227228 return null ;
@@ -233,7 +234,7 @@ public static function getHashMethod(string $password): ?string
233234 /**
234235 * Attempt to retrieve the hash method and algorithm used for the password.
235236 */
236- public static function getHashMethodAndAlgo (string $ password ): ?array
237+ public static function getHashMethodAndAlgo (#[SensitiveParameter] string $ password ): ?array
237238 {
238239 if (! preg_match ('/^\{(\w+)\}\$([0-9a-z]{1})\$/ ' , $ password , $ matches )) {
239240 return null ;
@@ -247,7 +248,7 @@ public static function getHashMethodAndAlgo(string $password): ?array
247248 *
248249 * @throws LdapRecordException
249250 */
250- public static function getSalt (string $ encryptedPassword ): string
251+ public static function getSalt (#[SensitiveParameter] string $ encryptedPassword ): string
251252 {
252253 // crypt() methods.
253254 if (preg_match ('/^\{(\w+)\}(\$.*\$).*$/ ' , $ encryptedPassword , $ matches )) {
0 commit comments