Files from 27-03-26: Password Hashing and checks for password stregnth
This commit is contained in:
47
Zweites Jahr/unterrichts_projekt/portal/functions.php
Normal file
47
Zweites Jahr/unterrichts_projekt/portal/functions.php
Normal 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);
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user