2
0

Files from 27-03-26: Password Hashing and checks for password stregnth

This commit is contained in:
Daniel
2026-03-27 08:14:22 +01:00
parent 9238cba6f3
commit 39c8234b6d
3 changed files with 65 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
<?php
/**
* Check password strength (1/2)
* @param string $password password to check
* @return bool - true or false
*/
function isStrongPassword(string $password): bool
{
// min. 8 Zeichen, 1 Kleinbuchstabe, 1 Großbuchstabe, 1 Zahl, 1 Sonderzeichen
return (bool) preg_match(
'/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z0-9]).{8,}$/',
$password
);
}
/**
* Check password strength (2/2)
* @param string $password password to check
* @return bool - true or false
*/
function checkPasswordStrength(string $password): bool {
$strength = 0;
//check length
$length = strlen($password);
if($length >= 8) $strength++;
//check if caps
if(preg_match('/[A-Z]/', $password)) $strength++;
//check if lower cases
if(preg_match('/[a-z]/', $password)) $strength++;
//check if numbers
if(preg_match('/[0-9]/', $password)) $strength++;
//check if spec. chars
if(preg_match('/[^a-zA-Z0-9]/', $password)) $strength++;
return($strength >= 5);
}