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

    
194
} // end of class PasswordHash
(4-4/32)