FusionDirectory
class_passwordMethodCrypt.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4 
5  Copyright (C) 2003-2010 Cajus Pollmeier
6  Copyright (C) 2011-2019 FusionDirectory
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
33 {
34 
38  function __construct ()
39  {
40  }
41 
47  public function is_available (): bool
48  {
49  return function_exists('crypt');
50  }
51 
60  public function generate_hash (string $pwd, bool $locked = FALSE): string
61  {
62  $salt = '';
63 
64  if ($this->hash == "crypt/standard-des") {
65  $salt = "";
66  for ($i = 0; $i < 2; $i++) {
67  $salt .= get_random_char();
68  }
69  } elseif ($this->hash == "crypt/enhanced-des") {
70  $salt = "_";
71  for ($i = 0; $i < 8; $i++) {
72  $salt .= get_random_char();
73  }
74  } elseif ($this->hash == "crypt/md5") {
75  $salt = "\$1\$";
76  for ($i = 0; $i < 8; $i++) {
77  $salt .= get_random_char();
78  }
79  $salt .= "\$";
80  } elseif ($this->hash == "crypt/blowfish") {
81  $salt = "\$2a\$07\$";
82  for ($i = 0; $i < CRYPT_SALT_LENGTH; $i++) {
83  $salt .= get_random_char();
84  }
85  $salt .= "\$";
86  } elseif ($this->hash == "crypt/sha-256") {
87  $salt = "\$5\$";
88  for ($i = 0; $i < 16; $i++) {
89  $salt .= get_random_char();
90  }
91  $salt .= "\$";
92  } elseif ($this->hash == "crypt/sha-512") {
93  $salt = "\$6\$";
94  for ($i = 0; $i < 16; $i++) {
95  $salt .= get_random_char();
96  }
97  $salt .= "\$";
98  }
99 
100  return '{CRYPT}'.($locked ? '!' : '').crypt($pwd, $salt);
101  }
102 
103  function checkPassword ($pwd, $hash): bool
104  {
105  // Not implemented
106  return FALSE;
107  }
108 
112  static function get_hash_name ()
113  {
114  $hashes = [];
115  if (CRYPT_STD_DES == 1) {
116  $hashes[] = "crypt/standard-des";
117  }
118 
119  if (CRYPT_EXT_DES == 1) {
120  $hashes[] = "crypt/enhanced-des";
121  }
122 
123  if (CRYPT_MD5 == 1) {
124  $hashes[] = "crypt/md5";
125  }
126 
127  if (CRYPT_BLOWFISH == 1) {
128  $hashes[] = "crypt/blowfish";
129  }
130 
131  if (CRYPT_SHA256 == 1) {
132  $hashes[] = "crypt/sha-256";
133  }
134 
135  if (CRYPT_SHA512 == 1) {
136  $hashes[] = "crypt/sha-512";
137  }
138 
139  return $hashes;
140  }
141 
149  static function _extract_method ($password_hash): string
150  {
151  if (!preg_match('/^{crypt}/i', $password_hash)) {
152  return "";
153  }
154 
155  $password_hash = preg_replace('/^{[^}]+}!?/', '', $password_hash);
156 
157  if (preg_match("/^[a-zA-Z0-9.\/][a-zA-Z0-9.\/]/", $password_hash)) {
158  return "crypt/standard-des";
159  }
160 
161  if (preg_match("/^_[a-zA-Z0-9.\/]/", $password_hash)) {
162  return "crypt/enhanced-des";
163  }
164 
165  if (preg_match('/^\$1\$/', $password_hash)) {
166  return "crypt/md5";
167  }
168 
169  if (preg_match('/^(\$2\$|\$2a\$)/', $password_hash)) {
170  return "crypt/blowfish";
171  }
172 
173  if (preg_match('/^\$5\$/', $password_hash)) {
174  return "crypt/sha-256";
175  }
176 
177  if (preg_match('/^\$6\$/', $password_hash)) {
178  return "crypt/sha-512";
179  }
180 
181  return "";
182  }
183 }
get_random_char()
Returns a random char.
Definition: functions.inc:1409
This class contains all the functions for crypt password methods.
generate_hash(string $pwd, bool $locked=FALSE)
Generate template hash.
__construct()
passwordMethodCrypt Constructor
static get_hash_name()
Get the hash name.
This class contains all the basic function for password methods.
static _extract_method($password_hash)
Extract a method.