FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Password.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Password extends \Rsi\Fred\User\Authenticator{
6 
7  public $passwordAlgo = PASSWORD_DEFAULT;
8 
9  public $strengthKeyboard = [
10  ['`~','1!','2@','3#','4$','5%','6^','7&','8*','9(','0)','-_','=+'],
11  [null,'qQ','wW','eE','rR','tT','yY','uU','iI','oO','pP','[{',']}','\\|'],
12  [null,'aA','sS','dD','fF','gG','hH','jJ','kK','lL',';:','\'"'],
13  [null,'zZ','xX','cC','vV','bB','nN','mM',',<','.>','/?']
14  ];
15  public $strengthCharSets = [ //chars => points
16  'abcdefghijklmnopqrstuvwxyz' => 1,
17  'ABCDEFGHIJKLMNOPQRSTUVWXYZ' => 3,
18  '0123456789' => 4,
19  '`~!@#$%^&*()\\-_=+[{]}\|;:\'",<.>/?' => 10,
20  null => 25 //all other chars
21  ];
22  public $strengthCharSetChangeScore = 5;
23 
24  protected function check(){
25  return null;
26  }
27  /**
28  * Create a password hash.
29  * @param string $password
30  * @return string Password hash.
31  * @see http://php.net/password_hash
32  */
33  public function hash($password){
34  return password_hash($password,$this->passwordAlgo);
35  }
36  /**
37  * Check if a hash needs rehashing.
38  * @param string $hash Stored hash.
39  * @return bool True if rehashing is advised.
40  * @see http://php.net/password_needs_rehash
41  */
42  public function needsRehash($hash){
43  return password_needs_rehash($hash,$this->passwordAlgo);
44  }
45  /**
46  * Verify a password.
47  * @param string $password User password.
48  * @param string $hash Stored hash.
49  * @return bool True if the password and hash match.
50  * @see http://php.net/password_verify
51  */
52  public function verify($password,$hash){
53  $this->verified(password_verify($password,$hash));
54  return $this->checked;
55  }
56  /**
57  * Check if a password is a 'known' password (found on public available lists).
58  * @param string $password User password.
59  * @return bool True if the password was found on a list.
60  */
61  public function known($password){
62  return \Rsi\Record::get($this->component('services')->password($password),'known');
63  }
64  /**
65  * Calculate password strength.
66  * @param string $password User password.
67  * @return int Calculated strength, based on score settings.
68  */
69  public function strength($password){
70  $score = $prev_char = $prev_char_set = null;
71  foreach(str_split($password) as $index => $char){
72  $adjacent = null;
73  if($prev_char !== null) foreach($this->strengthKeyboard as $r => $row) foreach($row as $k => $key) if(strpos($key,$char) !== false){
74  for($i = -1; $i <= 1; $i++) for($j = -1; $j <= 1; $j++) if(($i >= 0) || ($j >= 0))
75  $adjacent .= \Rsi\Record::get($this->strengthKeyboard,[$r + $i,$k + $j]);
76  if(strpos($adjacent,$prev_char) === false) $adjacent = false;
77  break 2;
78  }
79  if($adjacent) $score++;
80  else{
81  $score += $index;
82  foreach($this->strengthCharSets as $char_set => $char_set_score) if(!$char_set || (strpos($char_set,$char) !== false)){
83  $score += $char_set_score;
84  if($prev_char_set && ($char_set != $prev_char_set)) $score += $this->strengthCharSetChangeScore;
85  $prev_char_set = $char_set;
86  break;
87  }
88  }
89  $prev_char = $char;
90  }
91  return $score;
92  }
93 
94 }
known($password)
Check if a password is a &#39;known&#39; password (found on public available lists).
Definition: Password.php:61