Passed
Push — master ( ad2baf...e1d221 )
by Florian
01:09
created

Md5PasswordHasher::hash()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 3
cts 6
cp 0.5
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 4.125
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\PasswordHasher;
15
16
/**
17
 * Md5 Password Hasher
18
 *
19
 * WARNING: This is UNSAFE and should NOT be used anymore!!! md5 has been
20
 * compromised! It is no longer a secure hash algorithm!
21
 *
22
 * This hasher just exists for legacy reasons and is thought to be used with
23
 * the Fallback hasher, to update the passwords transparently to a secure hash.
24
 */
25
class Md5PasswordHasher extends AbstractPasswordHasher
26
{
27
    /**
28
     * Raw sha1 output
29
     *
30
     * @var bool
31
     */
32
    protected $rawOutput = false;
33
34
    /**
35
     * Sets the raw output mode of sha1
36
     *
37
     * @link http://php.net/manual/en/function.sha1.php
38
     * @param bool $rawOutput Raw output
39
     * @return $this
40
     */
41
    public function setRawOutput(bool $rawOutput): self
42
    {
43
        $this->rawOutput = $rawOutput;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Generates password hash.
50
     *
51
     * @param string|array $password Plain text password to hash or array of data
52
     *   required to generate password hash.
53
     * @return string Password hash
54
     */
55 4
    public function hash($password): string
56
    {
57 4
        if (!empty($this->salt)) {
58
            if ($this->saltPosition === self::SALT_BEFORE) {
59
                $password = $this->salt . $password;
0 ignored issues
show
Bug introduced by
Are you sure $password of type string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                $password = $this->salt . /** @scrutinizer ignore-type */ $password;
Loading history...
60
            } else {
61
                $password = $password . $this->salt;
62
            }
63
        }
64
65 4
        return $this->callHashFunction($password);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type array; however, parameter $password of Phauthentic\PasswordHash...her::callHashFunction() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        return $this->callHashFunction(/** @scrutinizer ignore-type */ $password);
Loading history...
66
    }
67
68
    /**
69
     * Calls the actual php method that will do the hashing
70
     *
71
     * @param string $password Password string
72
     * @return string
73
     */
74 2
    protected function callHashFunction($password): string
75
    {
76 2
        return md5($password, $this->rawOutput);
77
    }
78
79
    /**
80
     * Check hash. Generate hash from user provided password string or data array
81
     * and check against existing hash.
82
     *
83
     * @param string|array $password Plain text password to hash or data array.
84
     * @param string $hashedPassword Existing hashed password.
85
     * @return bool True if hashes match else false.
86
     */
87 2
    public function check($password, string $hashedPassword): bool
88
    {
89 2
        return $this->hash($password) === $hashedPassword;
90
    }
91
}
92