FusionDirectory
 All Data Structures Files Functions Variables
class_password-methods-crypt.inc
Go to the documentation of this file.
1 <?php
2 
3 /*
4  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
5  Copyright (C) 2003-2010 Cajus Pollmeier
6  Copyright (C) 2011-2016 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  function is_available()
48  {
49  return function_exists('crypt');
50  }
51 
57  function generate_hash($pwd)
58  {
59  if ($this->hash == "crypt/standard-des") {
60  $salt = "";
61  for ($i = 0; $i < 2; $i++) {
62  $salt .= get_random_char();
63  }
64  }
65 
66  if ($this->hash == "crypt/enhanced-des") {
67  $salt = "_";
68  for ($i = 0; $i < 8; $i++) {
69  $salt .= get_random_char();
70  }
71  }
72 
73  if ($this->hash == "crypt/md5") {
74  $salt = "\$1\$";
75  for ($i = 0; $i < 8; $i++) {
76  $salt .= get_random_char();
77  }
78  $salt .= "\$";
79  }
80 
81  if ($this->hash == "crypt/blowfish") {
82  $salt = "\$2a\$07\$";
83  for ($i = 0; $i < CRYPT_SALT_LENGTH; $i++) {
84  $salt .= get_random_char();
85  }
86  $salt .= "\$";
87  }
88 
89  if ($this->hash == "crypt/sha-256") {
90  $salt = "\$5\$";
91  for ($i = 0; $i < 16; $i++) {
92  $salt .= get_random_char();
93  }
94  $salt .= "\$";
95  }
96 
97  if ($this->hash == "crypt/sha-512") {
98  $salt = "\$6\$";
99  for ($i = 0; $i < 16; $i++) {
100  $salt .= get_random_char();
101  }
102  $salt .= "\$";
103  }
104 
105  return "{CRYPT}".crypt($pwd, $salt);
106  }
107 
108  function checkPassword($pwd, $hash)
109  {
110  // Not implemented
111  return FALSE;
112  }
113 
117  static function get_hash_name()
118  {
119  $hashes = array();
120  if (CRYPT_STD_DES == 1) {
121  $hashes[] = "crypt/standard-des";
122  }
123 
124  if (CRYPT_EXT_DES == 1) {
125  $hashes[] = "crypt/enhanced-des";
126  }
127 
128  if (CRYPT_MD5 == 1) {
129  $hashes[] = "crypt/md5";
130  }
131 
132  if (CRYPT_BLOWFISH == 1) {
133  $hashes[] = "crypt/blowfish";
134  }
135 
136  if (CRYPT_SHA256 == 1) {
137  $hashes[] = "crypt/sha-256";
138  }
139 
140  if (CRYPT_SHA512 == 1) {
141  $hashes[] = "crypt/sha-512";
142  }
143 
144  return $hashes;
145  }
146 
152  static function _extract_method($classname, $password_hash)
153  {
154  if (!preg_match('/^{crypt}/i', $password_hash)) {
155  return "";
156  }
157 
158  $password_hash = preg_replace('/^{[^}]+}!?/', '', $password_hash);
159 
160  if (preg_match("/^[a-zA-Z0-9.\/][a-zA-Z0-9.\/]/", $password_hash)) {
161  return "crypt/standard-des";
162  }
163 
164  if (preg_match("/^_[a-zA-Z0-9.\/]/", $password_hash)) {
165  return "crypt/enhanced-des";
166  }
167 
168  if (preg_match('/^\$1\$/', $password_hash)) {
169  return "crypt/md5";
170  }
171 
172  if (preg_match('/^(\$2\$|\$2a\$)/', $password_hash)) {
173  return "crypt/blowfish";
174  }
175 
176  if (preg_match('/^\$5\$/', $password_hash)) {
177  return "crypt/sha-256";
178  }
179 
180  if (preg_match('/^\$6\$/', $password_hash)) {
181  return "crypt/sha-512";
182  }
183 
184  return "";
185  }
186 }
187 ?>
get_random_char()
Returns a random char.
Definition: functions.inc:2401
generate_hash($pwd)
Generate template hash.
This class contains all the functions for crypt password methods.
__construct()
passwordMethodCrypt Constructor
static get_hash_name()
Get the hash name.
This class contains all the basic function for password methods.
static _extract_method($classname, $password_hash)
Extract a method.