Project

General

Profile

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
 * Description of Password
20
 * *****************************************************************************
21
 * This class is interfacing the Portable PHP password hashing framework.
22
 * Version 0.4 / ISTeam Rev. 0.1
23
 * ISTeam changes: added SHA-256, SHA-512 (2012/10/27 Werner v.d. Decken)
24
 * *****************************************************************************
25
 *
26
 * @category     WBCore
27
 * @package      WBCore_Security
28
 * @author       Werner v.d. Decken <wkl@isteam.de>
29
 * @copyright    Werner v.d. Decken <wkl@isteam.de>
30
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
31
 * @version      1.0.3
32
 * @revision     $Revision: 1901 $
33
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/Password.php $
34
 * @lastmodified $Date: 2013-04-19 11:43:44 +0200 (Fri, 19 Apr 2013) $
35
 * @since        Datei vorhanden seit Release 1.2.0
36
 */
37

    
38
// use \vendors\phpass\PasswordHash;
39
if(!class_exists('PasswordHash')) {
40
	include(dirname(dirname(__FILE__)).'/include/phpass/PasswordHash.php'); 
41
}
42

    
43

    
44
class Password extends PasswordHash
45
//class Password extends v_phpass_PasswordHash
46
{
47

    
48
	const MIN_CRYPT_LOOPS     =  6;  // minimum numbers of loops is 2^6 (64) very, very quick
49
	const MAX_CRYPT_LOOPS     = 31;  // maximum numbers of loops is 2^31 (2,147,483,648) extremely slow
50
	const DEFAULT_CRYPT_LOOPS = 12;  // default numbers of loopf is 2^12 (4096) a good average
51

    
52
	const HASH_TYPE_PORTABLE  = true;  // use MD5 only
53
	const HASH_TYPE_AUTO      = false; // select highest available crypting methode
54

    
55
	const MIN_PW_LENGTH       =   6;
56
	const MAX_PW_LENGTH       = 100;
57
	const DEFAULT_PW_LENGTH   =  10;
58

    
59
	const PW_USE_LOWERCHAR    = 0x0001; // use lower chars
60
	const PW_USE_UPPERCHAR    = 0x0002; // use upper chars
61
	const PW_USE_DIGITS       = 0x0004; // use numeric digits
62
	const PW_USE_SPECIAL      = 0x0008; // use special chars
63
	const PW_USE_ALL          = 0xFFFF; // use all possibilities
64

    
65
/**
66
 * 
67
 * @param int number of iterations as exponent of 2 (must be between 4 and 31)
68
 * @param bool TRUE = use MD5 only | FALSE = automatic
69
 */
70
	public function __construct($iIterationCountLog2 = self::DEFAULT_CRYPT_LOOPS, $bPortableHashes = self::HASH_TYPE_AUTO)
71
	{
72
		parent::__construct($iIterationCountLog2, $bPortableHashes);
73
	}
74
/**
75
 * @param string password to hash
76
 * @return string generated hash. Null if failed.
77
 */
78
	public function hashPassword($sPassword)
79
	{
80
		$sNewHash = parent::HashPassword($sPassword);
81
		return ($sNewHash == '*') ? null : $sNewHash;
82
	}
83
/**
84
 * @param string Password to test against given Hash
85
 * @param string existing stored hash
86
 * @return bool true if PW matches the stored hash
87
 */
88
	public function checkPassword($sPassword, $sStoredHash)
89
	{
90
		// compatibility layer for deprecated, simple and old MD5 hashes
91
		if(preg_match('/^[0-9a-f]{32}$/si', $sStoredHash)) {
92
			return (md5($sPassword) === $sStoredHash);
93
		}
94
		return parent::CheckPassword($sPassword, $sStoredHash);
95
	}
96
/**
97
 * generate a case sensitive mnemonic password including numbers and special chars
98
 * makes no use of confusing characters like 'O' and '0' and so on.
99
 * @param int length of the generated password. default = DEFAULT_PW_LENGTH
100
 * @param int defines which elemets are used to generate a password. Default = PW_USE_ALL
101
 * @return string
102
 */
103
	public static function createNew($iLength = self::DEFAULT_PW_LENGTH, $iElements = self::PW_USE_ALL)
104
	{
105
		$aChars = array(
106
			array('b','c','d','f','g','h','j','k','m','n','p','q','r','s','t','v','w','x','y','z'),
107
			array('B','C','D','F','G','H','J','K','M','N','P','Q','R','S','T','V','W','X','Y','Z'),
108
			array('a','e','i','o','u'),
109
			array('A','E','U'),
110
			array('!','-','@','_',':','.','+','%','/','*')
111
		);
112
		$iElements = ($iElements & self::PW_USE_ALL) == 0 ? self::PW_USE_ALL : $iElements;
113
		if(($iLength < self::MIN_PW_LENGTH) || ($iLength > self::MAX_PW_LENGTH)) {
114
			$iLength = self::DEFAULT_PW_LENGTH;
115
		}
116
	// at first create random arrays of lowerchars and uperchars
117
	// alternating between vowels and consonants
118
		$aUpperCase = array();
119
		$aLowerCase = array();
120
		for($x = 0; $x < ceil($iLength / 2); $x++) {
121
			// consonants
122
			$y = rand(1000, 10000) % sizeof($aChars[0]);
123
			$aLowerCase[] = $aChars[0][$y];
124
			$y = rand(1000, 10000) % sizeof($aChars[1]);
125
			$aUpperCase[] = $aChars[1][$y];
126
			// vowels
127
			$y = rand(1000, 10000) % sizeof($aChars[2]);
128
			$aLowerCase[] = $aChars[2][$y];
129
			$y = rand(1000, 10000) % sizeof($aChars[3]);
130
			$aUpperCase[] = $aChars[3][$y];
131
		}
132
	// create random arrays of numeric digits 2-9 and  special chars
133
		$aDigits       = array();
134
		$aSpecialChars = array();
135
		for($x = 0; $x < $iLength; $x++) {
136
			$aDigits[] = (rand(1000, 10000) % 8) + 2;
137
			$aSpecialChars[] = $aChars[4][rand(1000, 10000) % sizeof($aChars[4])];
138
		}
139
		// take string or merge chars depending from $iElements
140
		$aPassword = array();
141
		$bMerge = false;
142
		if($iElements & self::PW_USE_LOWERCHAR) {
143
			$aPassword = $aLowerCase;
144
			$bMerge = true;
145
		}
146
		if($iElements & self::PW_USE_UPPERCHAR) {
147
			if($bMerge) {
148
				$iNumberOfUpperChars = rand(1000, 10000) % ($iLength);
149
				$aPassword = self::_mergeIntoPassword($aPassword, $aUpperCase, $iNumberOfUpperChars);
150
			}else {
151
				$aPassword = $aUpperCase;
152
				$bMerge = true;
153
			}
154
		}
155
		if($iElements & self::PW_USE_DIGITS) {
156
			if($bMerge) {
157
				$iNumberOfDigits = (rand(1000, 10000) % ceil($iLength / 2.5));
158
				$iNumberOfDigits = $iNumberOfDigits ? $iNumberOfDigits : 1;
159
				$aPassword = self::_mergeIntoPassword($aPassword, $aDigits, $iNumberOfDigits);
160
			}else {
161
				$aPassword = $aDigits;
162
				$bMerge = true;
163
			}
164
		}
165
		if($iElements & self::PW_USE_SPECIAL) {
166
			if($bMerge) {
167
				$iNumberOfSpecialChars = rand(1000, 10000) % ceil($iLength / 5);
168
				$iNumberOfSpecialChars = $iNumberOfSpecialChars ? $iNumberOfSpecialChars : 1;
169
				$aPassword = self::_mergeIntoPassword($aPassword, $aSpecialChars, $iNumberOfSpecialChars);
170
			}else {
171
				$aPassword = $aSpecialChars;
172
				$bMerge = true;
173
			}
174
		}
175
		$sPassword = implode('', array_slice($aPassword, 0, $iLength));
176
		return $sPassword;
177
	}
178
/**
179
 * merges $iCount chars from $aInsert randomly into $aPassword
180
 * @param array $aPassword
181
 * @param array $aInsert
182
 * @param integer $iCount
183
 * @return array
184
 */
185
	private static function _mergeIntoPassword($aPassword, $aInsert, $iCount)
186
	{
187
		$aListOfIndexes = array();
188
		while(sizeof($aListOfIndexes) < $iCount) {
189
			$x = rand(1000, 10000) % sizeof($aInsert);
190
			$aListOfIndexes[$x] = $x;
191
		}
192
		foreach($aListOfIndexes as $x) {
193
			$aPassword[$x] = $aInsert[$x];
194
		}
195
		return $aPassword;
196
	}
197

    
198
} // end of class PasswordHash
(4-4/33)