Skip to content

Commit 4f643af

Browse files
committed
add security helper from courier
1 parent 8a57cad commit 4f643af

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* User: gabidj
4+
* Date: 2019-07-12
5+
* Time: 18:08
6+
*/
7+
8+
namespace Devchain\LaravelToolkit\Helpers\Model;
9+
10+
class SecurityHelper
11+
{
12+
public static function generatePassword($length = 12, $characterTypes = ['ludsD'])
13+
{
14+
$sets = [];
15+
$addDashes = false;
16+
if (strpos($characterTypes, 'l') !== false)
17+
$sets[] = 'abcdefghjkmnpqrstuvwxyz';
18+
if (strpos($characterTypes, 'u') !== false)
19+
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
20+
if (strpos($characterTypes, 'd') !== false)
21+
$sets[] = '23456789';
22+
if (strpos($characterTypes, 's') !== false)
23+
$sets[] = '!@#$%&*?';
24+
if (strpos($characterTypes, '-') !== false)
25+
$addDashes = true;
26+
27+
28+
$all = '';
29+
$password = '';
30+
foreach ($sets as $set) {
31+
$password .= $set[array_rand(str_split($set))];
32+
$all .= $set;
33+
}
34+
35+
$all = str_split($all);
36+
for ($i = 0; $i < $length - count($sets); $i++)
37+
$password .= $all[array_rand($all)];
38+
39+
$password = str_shuffle($password);
40+
41+
if (!$addDashes) {
42+
return $password;
43+
}
44+
45+
$dash_len = floor(sqrt($length));
46+
$dash_str = '';
47+
while (strlen($password) > $dash_len) {
48+
$dash_str .= substr($password, 0, $dash_len) . '-';
49+
$password = substr($password, $dash_len);
50+
}
51+
$dash_str .= $password;
52+
return $dash_str;
53+
}
54+
}

0 commit comments

Comments
 (0)