Passed
Push — master ( 13644f...5aa358 )
by Wilmer
02:12
created

Password::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace terabytesoft\helpers;
4
5
use yii\base\Security;
6
7
/**
8
 * Class Password
9
 *
10
 * Helper security methods
11
 **/
12
class Password
13
{
14
    /**
15
     * @var Security $security
16
     **/
17
    protected $security;
18
19
    /**
20
     * __construct
21
     **/
22 3
    public function __construct()
23
    {
24 3
        $this->security = new Security();
25 3
    }
26
27
    /**
28
     * hash
29
     *
30
     * Wrapper for yii security helper method
31
     **/
32 3
    public function hash(string $password, int $algo, array $params = []): string
33
    {
34 3
        return password_hash($password, $algo, $params);
35
    }
36
37
38
    /**
39
     * validate
40
     *
41
     * wrapper for yii security helper method
42
     **/
43 3
    public function validate(string $password, string $hash): bool
44
    {
45 3
        return password_verify($password, $hash);
46
    }
47
48
    /**
49
     * generate
50
     *
51
     * generates user-friendly random password containing at least one lower case letter, one uppercase letter and one
52
     * digit. The remaining characters in the password are chosen at random from those three sets
53
     *
54
     * @see https://gist.github.com/tylerhall/521810
55
     **/
56 3
    public function generate(int $length): string
57
    {
58
        $sets = [
59 3
            'abcdefghjkmnpqrstuvwxyz',
60
            'ABCDEFGHJKMNPQRSTUVWXYZ',
61
            '23456789',
62
        ];
63 3
        $all = '';
64 3
        $password = '';
65 3
        foreach ($sets as $set) {
66 3
            $password .= $set[array_rand(str_split($set))];
67 3
            $all .= $set;
68
        }
69
70 3
        $all = str_split($all);
71 3
        for ($i = 0; $i < $length - count($sets); $i++) {
72 3
            $password .= $all[array_rand($all)];
73
        }
74
75 3
        $password = str_shuffle($password);
76
77 3
        return $password;
78
    }
79
}
80