Password::verify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Controlabs\Helper;
4
5
class Password
6
{
7
    protected const SHA_512 = 'sha512';
8
9 1
    public function encrypt(string $password, string $salt = null, string $algorithm = self::SHA_512): PasswordData
10
    {
11 1
        if (!$salt) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $salt of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
12 1
            $salt = hash($algorithm, uniqid(strval(mt_rand(1, mt_getrandmax())), true));
13
        }
14
15 1
        $password = hash($algorithm, $password . $salt);
16
17 1
        return new PasswordData($password, $salt);
18
    }
19
20 1
    public function verify(string $encrypted, string $password, string $salt, string $algorithm = self::SHA_512): bool
21
    {
22 1
        return $encrypted === $this->encrypt($password, $salt, $algorithm)->password();
23
    }
24
}
25