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: 1900 $
33
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/Password.php $
34
 * @lastmodified $Date: 2013-04-18 22:05:18 +0200 (Thu, 18 Apr 2013) $
35
 * @since        Datei vorhanden seit Release 1.2.0
36
 */
37

    
38
// use \vendors\phpass\PasswordHash;
39

    
40
class Password extends PasswordHash
41
//class Password extends v_phpass_PasswordHash
42
{
43

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

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