Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 81 lines (70 sloc) 2.712 kb
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
1 <?php
622c60d @gneustaetter Changed directory structure
authored
2 namespace gUtils;
3
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
4 /*
b750176 @gneustaetter Changed from BSD to Apache 2 license
authored
5 gutils PasswordHelper
6 Copyright 2011 Greg Neustaetter
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
7
b750176 @gneustaetter Changed from BSD to Apache 2 license
authored
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
11
b750176 @gneustaetter Changed from BSD to Apache 2 license
authored
12 http://www.apache.org/licenses/LICENSE-2.0
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
13
b750176 @gneustaetter Changed from BSD to Apache 2 license
authored
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
19 */
20
21 class PasswordHelper {
22 private $badHashes = array('*0', '*1');
23 private $base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
24
25 public function generateHash($password,$cost=10) {
26 if(CRYPT_BLOWFISH != 1) {
622c60d @gneustaetter Changed directory structure
authored
27 throw new \Exception('This version of PHP does not support Blowfish hashing. Try PHP version 5.3 or greater');
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
28 }
29 $salt = $this->generateBlowfishSalt($cost);
30 $hashed = crypt($password, $salt);
31 if(in_array($hashed, $this->badHashes)) {
622c60d @gneustaetter Changed directory structure
authored
32 throw new \Exception('Password hashing failed with value: ' . $hashed);
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
33 } else {
34 return $hashed;
35 }
36 }
37
38 public function compareToHash($password,$hash) {
39 return (crypt($password,$hash) == $hash);
40 }
41
42 public function generateRandomString($length, $validChars) {
43 $string = '';
44 $maxIndex = strlen($validChars) - 1;
45 for($i=0;$i<$length;$i++) {
46 $string .= $validChars[mt_rand(0,$maxIndex)];
47 }
48 return $string;
49 }
50
51 public function generateBlowfishSalt($cost=10) {
52 if(($cost < 4) || ($cost > 31)) {
622c60d @gneustaetter Changed directory structure
authored
53 throw new \Exception('Cost must be an integer in the range 4-31');
a58f398 @gneustaetter initial checkin of Console, PasswordHelper, and RecursiveFileExtensionF...
authored
54 }
55 $salt = '$2a$';
56 $salt .= ($cost < 10) ? '0' : '';
57 $salt .= $cost . '$';
58 $salt .= $this->generateRandomString(22, $this->base64Chars);
59 return $salt;
60 }
61
62 public function generateRandomPassword($length=8, $validChars='ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789!@#$%^&*') {
63 return $this->generateRandomString($length, $validChars);
64 }
65
66 public function checkPasswordComplexity($password, $minLength=8, $maxLength=50, $patterns=array('/[a-z]/', '/[A-Z]/', '/[0-9]/', '/[!@#$%\^\&\(\)\+=]/'), $minPatternMatches=3) {
67 if(count($patterns) < $minPatternMatches) {
68 throw Exception('The number of patterns must be greater than or equal to the minimum number of pattern matches');
69 }
70 if((strlen($password) >= $minLength) && (strlen($password) <= $maxLength)) {
71 $patternMatches = 0;
72 foreach($patterns as $pattern) {
73 if(preg_match($pattern,$password)) {
74 $patternMatches++;
75 }
76 }
77 return $patternMatches >= $minPatternMatches;
78 }
79 return false;
80 }
81 }
Something went wrong with that request. Please try again.