Completed
Push — master ( 831b77...b56796 )
by Antonio
02:41
created

SecurityHelper::generatePasswordHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Helper;
13
14
use yii\base\Security;
15
16
class SecurityHelper
17
{
18
    /**
19
     * @var Security
20
     */
21
    protected $security;
22
23 9
    public function __construct(Security $security)
24
    {
25 9
        $this->security = $security;
26 9
    }
27
28
    /**
29
     * Generates a secure hash from a password and a random salt.
30
     *
31
     * @param string   $password
32
     * @param null|int $cost
33
     *
34
     * @return string
35
     */
36 3
    public function generatePasswordHash($password, $cost = null)
37
    {
38 3
        return $this->security->generatePasswordHash($password, $cost);
39
    }
40
41 5
    public function generateRandomString($length = 32)
42
    {
43 5
        return $this->security->generateRandomString($length);
44
    }
45
46 5
    public function validatePassword($password, $hash)
47
    {
48 5
        return $this->security->validatePassword($password, $hash);
49
    }
50
51 1
    public function generatePassword($length)
52
    {
53
        $sets = [
54 1
            'abcdefghjkmnpqrstuvwxyz',
55
            'ABCDEFGHJKMNPQRSTUVWXYZ',
56
            '23456789',
57
        ];
58 1
        $all = '';
59 1
        $password = '';
60 1
        foreach ($sets as $set) {
61 1
            $password .= $set[array_rand(str_split($set))];
62 1
            $all .= $set;
63
        }
64
65 1
        $all = str_split($all);
66 1
        for ($i = 0; $i < $length - count($sets); ++$i) {
67 1
            $password .= $all[array_rand($all)];
68
        }
69
70 1
        $password = str_shuffle($password);
71
72 1
        return $password;
73
    }
74
}
75