1
|
<?php
|
2
|
/**
|
3
|
* Copyright (C) 2012 Werner v.d. Decken <wkl@isteam.de>
|
4
|
*
|
5
|
* This program is free software: you can redistribute it and/or modify
|
6
|
* it under the terms of the GNU General Public License as published by
|
7
|
* the Free Software Foundation, either version 3 of the License, or
|
8
|
* (at your option) any later version.
|
9
|
*
|
10
|
* This program is distributed in the hope that it will be useful,
|
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
* GNU General Public License for more details.
|
14
|
*
|
15
|
* You should have received a copy of the GNU General Public License
|
16
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
*/
|
18
|
/**
|
19
|
* @category WBCore
|
20
|
* @package WBCore_Security
|
21
|
* @author Werner v.d. Decken <wkl@isteam.de>
|
22
|
* @copyright Werner v.d. Decken <wkl@isteam.de>
|
23
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
24
|
* @version 1.0.3
|
25
|
* @revision $Revision: 1906 $
|
26
|
* @link $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/Password.php $
|
27
|
* @lastmodified $Date: 2013-06-06 22:50:13 +0200 (Thu, 06 Jun 2013) $
|
28
|
* @since Datei vorhanden seit Release 1.2.0
|
29
|
* @description This class is interfacing the Portable PHP password hashing framework.<br />
|
30
|
* Version 0.4 / ISTeam Rev. 0.1<br />
|
31
|
* ISTeam changes: added SHA-256, SHA-512 (2012/10/27 Werner v.d. Decken)
|
32
|
*/
|
33
|
|
34
|
// backwardcompatibility for PHP 5.2.2 + WB2.8.x
|
35
|
if(!class_exists('PasswordHash')) {
|
36
|
include(dirname(dirname(__FILE__)).'/include/phpass/PasswordHash.php');
|
37
|
}
|
38
|
|
39
|
|
40
|
class Password extends PasswordHash
|
41
|
//class Password extends vendors\phpass\PasswordHash
|
42
|
{
|
43
|
|
44
|
const CRYPT_LOOPS_MIN = 6; // minimum numbers of loops is 2^6 (64) very, very quick
|
45
|
const CRYPT_LOOPS_MAX = 31; // maximum numbers of loops is 2^31 (2,147,483,648) extremely slow
|
46
|
const CRYPT_LOOPS_DEFAULT = 12; // default numbers of loopf is 2^12 (4096) a good average
|
47
|
|
48
|
const HASH_TYPE_PORTABLE = true; // use MD5 only
|
49
|
const HASH_TYPE_AUTO = false; // select highest available crypting methode
|
50
|
|
51
|
const PW_LENGTH_MIN = 6;
|
52
|
const PW_LENGTH_MAX = 100;
|
53
|
const PW_LENGTH_DEFAULT = 10;
|
54
|
|
55
|
const PW_USE_LOWERCHAR = 0x0001; // use lower chars
|
56
|
const PW_USE_UPPERCHAR = 0x0002; // use upper chars
|
57
|
const PW_USE_DIGITS = 0x0004; // use numeric digits
|
58
|
const PW_USE_SPECIAL = 0x0008; // use special chars
|
59
|
const PW_USE_ALL = 0xFFFF; // use all possibilities
|
60
|
|
61
|
/**
|
62
|
*
|
63
|
* @param int number of iterations as exponent of 2 (must be between 4 and 31)
|
64
|
* @param bool TRUE = use MD5 only | FALSE = automatic
|
65
|
*/
|
66
|
public function __construct($iIterationCountLog2 = self::CRYPT_LOOPS_DEFAULT, $bPortableHashes = self::HASH_TYPE_AUTO)
|
67
|
{
|
68
|
parent::__construct($iIterationCountLog2, $bPortableHashes);
|
69
|
}
|
70
|
/**
|
71
|
* make hash from password
|
72
|
* @param string password to hash
|
73
|
* @return string generated hash. Null if failed.
|
74
|
*/
|
75
|
public function makeHash($sPassword)
|
76
|
{
|
77
|
$sNewHash = parent::HashPassword($sPassword);
|
78
|
return ($sNewHash == '*') ? null : $sNewHash;
|
79
|
}
|
80
|
/**
|
81
|
* @param string Password to test against given Hash
|
82
|
* @param string existing stored hash
|
83
|
* @return bool true if PW matches the stored hash
|
84
|
*/
|
85
|
public function checkIt($sPassword, $sStoredHash)
|
86
|
{
|
87
|
// compatibility layer for deprecated, simple and old MD5 hashes
|
88
|
if(preg_match('/^[0-9a-f]{32}$/si', $sStoredHash)) {
|
89
|
return (md5($sPassword) === $sStoredHash);
|
90
|
}
|
91
|
return parent::CheckPassword($sPassword, $sStoredHash);
|
92
|
}
|
93
|
/**
|
94
|
* Check password for forbidden characters
|
95
|
* @param string password to test
|
96
|
* @return bool
|
97
|
*/
|
98
|
public static function isValid($sPassword)
|
99
|
{
|
100
|
$sBlackList = '\"\'\,\;\<\>\?\\\{\|\}\~ '
|
101
|
. '\x00-\x20\x22\x27\x2c\x3b\x3c\x3e\x3f\x5c\x7b-\x7f\xff';
|
102
|
$bRetval = !preg_match('/['.$sBlackList.']/si', $sPassword);
|
103
|
return $bRetval;
|
104
|
}
|
105
|
/**
|
106
|
* generate a case sensitive mnemonic password including numbers and special chars
|
107
|
* makes no use of confusing characters like 'O' and '0' and so on.
|
108
|
* @param int length of the generated password. default = PW_LENGTH_DEFAULT
|
109
|
* @param int defines which elemets are used to generate a password. Default = PW_USE_ALL
|
110
|
* @return string
|
111
|
*/
|
112
|
public static function createNew($iLength = self::PW_LENGTH_DEFAULT, $iElements = self::PW_USE_ALL)
|
113
|
{
|
114
|
$aChars = array(
|
115
|
array('b','c','d','f','g','h','j','k','m','n','p','q','r','s','t','v','w','x','y','z'),
|
116
|
array('B','C','D','F','G','H','J','K','M','N','P','Q','R','S','T','V','W','X','Y','Z'),
|
117
|
array('a','e','i','o','u'),
|
118
|
array('A','E','U'),
|
119
|
array('!','-','@','_',':','.','+','%','/','*','=')
|
120
|
);
|
121
|
$iElements = ($iElements & self::PW_USE_ALL) == 0 ? self::PW_USE_ALL : $iElements;
|
122
|
if(($iLength < self::PW_LENGTH_MIN) || ($iLength > self::PW_LENGTH_MAX)) {
|
123
|
$iLength = self::PW_LENGTH_DEFAULT;
|
124
|
}
|
125
|
// at first create random arrays of lowerchars and uperchars
|
126
|
// alternating between vowels and consonants
|
127
|
$aUpperCase = array();
|
128
|
$aLowerCase = array();
|
129
|
for($x = 0; $x < ceil($iLength / 2); $x++) {
|
130
|
// consonants
|
131
|
$i1 = rand(1000, 10000) % sizeof($aChars[0]);
|
132
|
$aLowerCase[] = $aChars[0][$i1];
|
133
|
$i2 = rand(1000, 10000) % sizeof($aChars[1]);
|
134
|
$aUpperCase[] = $aChars[1][$i2];
|
135
|
// vowels
|
136
|
$i3 = rand(1000, 10000) % sizeof($aChars[2]);
|
137
|
$aLowerCase[] = $aChars[2][$i3];
|
138
|
$i4 = rand(1000, 10000) % sizeof($aChars[3]);
|
139
|
$aUpperCase[] = $aChars[3][$i4];
|
140
|
}
|
141
|
// create random arrays of numeric digits 2-9 and special chars
|
142
|
$aDigits = array();
|
143
|
$aSpecialChars = array();
|
144
|
for($x = 0; $x < $iLength; $x++) {
|
145
|
$aDigits[] = (rand(1000, 10000) % 8) + 2;
|
146
|
$aSpecialChars[] = $aChars[4][rand(1000, 10000) % sizeof($aChars[4])];
|
147
|
}
|
148
|
// take string or merge chars depending from $iElements
|
149
|
$aPassword = array();
|
150
|
$bMerge = false;
|
151
|
if($iElements & self::PW_USE_LOWERCHAR) {
|
152
|
$aPassword = $aLowerCase;
|
153
|
$bMerge = true;
|
154
|
}
|
155
|
if($iElements & self::PW_USE_UPPERCHAR) {
|
156
|
if($bMerge) {
|
157
|
$iNumberOfUpperChars = rand(1000, 10000) % ($iLength);
|
158
|
$aPassword = self::_mergeIntoPassword($aPassword, $aUpperCase, $iNumberOfUpperChars);
|
159
|
}else {
|
160
|
$aPassword = $aUpperCase;
|
161
|
$bMerge = true;
|
162
|
}
|
163
|
}
|
164
|
if($iElements & self::PW_USE_DIGITS) {
|
165
|
if($bMerge) {
|
166
|
$x = (rand(1000, 10000) % ceil($iLength / 2.5));
|
167
|
$iNumberOfDigits = $x ? $x : 1;
|
168
|
$aPassword = self::_mergeIntoPassword($aPassword, $aDigits, $iNumberOfDigits);
|
169
|
}else {
|
170
|
$aPassword = $aDigits;
|
171
|
$bMerge = true;
|
172
|
}
|
173
|
}
|
174
|
if($iElements & self::PW_USE_SPECIAL) {
|
175
|
if($bMerge) {
|
176
|
$x = rand(1000, 10000) % ceil($iLength / 5);
|
177
|
$iNumberOfSpecialChars = $x ? $x : 1;
|
178
|
$aPassword = self::_mergeIntoPassword($aPassword, $aSpecialChars, $iNumberOfSpecialChars);
|
179
|
}else {
|
180
|
$aPassword = $aSpecialChars;
|
181
|
$bMerge = true;
|
182
|
}
|
183
|
}
|
184
|
$sPassword = implode('', array_slice($aPassword, 0, $iLength));
|
185
|
return $sPassword;
|
186
|
}
|
187
|
/**
|
188
|
* merges $iCount chars from $aInsert randomly into $aPassword
|
189
|
* @param array $aPassword
|
190
|
* @param array $aInsert
|
191
|
* @param integer $iCount
|
192
|
* @return array
|
193
|
*/
|
194
|
private static function _mergeIntoPassword($aPassword, $aInsert, $iCount)
|
195
|
{
|
196
|
$aListOfIndexes = array();
|
197
|
while(sizeof($aListOfIndexes) < $iCount) {
|
198
|
$x = rand(1000, 10000) % sizeof($aInsert);
|
199
|
$aListOfIndexes[$x] = $x;
|
200
|
}
|
201
|
foreach($aListOfIndexes as $x) {
|
202
|
$aPassword[$x] = $aInsert[$x];
|
203
|
}
|
204
|
return $aPassword;
|
205
|
}
|
206
|
|
207
|
} // end of class PasswordHash
|