Hash::password()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
class Hash
6
{
7
    /**
8
     * Application Object
9
     *
10
     * @var \System\Application
11
     */
12
    private $app;
13
14
    /**
15
     * Constructor
16
     *
17
     * @param \System\Application $app
18
     */
19
    public function __construct(Application $app)
20
    {
21
        $this->app = $app;
22
    }
23
24
    /**
25
     * Hash password
26
     *
27
     * @param $password
28
     * @return string
29
     */
30
    public function password(string $password)
31
    {
32
        $timeTarget = 0.05;
33
        $cost = 8;
34
        $password;
35
36
        do {
37
            $cost++;
38
            $start = microtime(true);
39
            $password = password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]);
40
            $end = microtime(true);
41
        } while (($end - $start) < $timeTarget);
42
43
        return $password;
44
    }
45
46
    /**
47
     * Check if the given password is verified with the given hash
48
     *
49
     * @param $password1
50
     * @param $password2
51
     * @return bool
52
     */
53
    public function passwordCheck(string $password1, string $password2)
54
    {
55
        return password_verify($password1, $password2);
56
    }
57
58
    /**
59
     * Hash the given string
60
     *
61
     * @param $string
62
     * @return string
63
     */
64
    public function hash(string $string)
65
    {
66
        return hash($_ENV['HASH_TYPE'], $string);
67
    }
68
69
    /**
70
     * Check if the given hashes are equal
71
     *
72
     * @param $hash1
73
     * @param $hash2
74
     * @return bool
75
     */
76
    public function hashCheck(string $hash1, string $hash2)
77
    {
78
        return hash_equals($hash1, $hash2);
79
    }
80
}
81