Passed
Pull Request — master (#5)
by Florian
02:22
created

Md5PasswordHasher::check()   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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 $password Plain text password to hash required to generate
52
     * password hash.
53
     * @return string Password hash
54
     */
55 4
    public function hash(string $password): string
56
    {
57 4
        $password = $this->saltPassword($password);
58
59 4
        return $this->callHashFunction($password);
60
    }
61
62
    /**
63
     * Calls the actual php method that will do the hashing
64
     *
65
     * @param string $password Password string
66
     * @return string
67
     */
68 2
    protected function callHashFunction(string $password): string
69
    {
70 2
        return md5($password, $this->rawOutput);
71
    }
72
73
    /**
74
     * Check hash. Generate hash from user provided password string
75
     * and check against existing hash.
76
     *
77
     * @param string $password Plain text password to hash.
78
     * @param string $hashedPassword Existing hashed password.
79
     * @return bool True if hashes match else false.
80
     */
81 2
    public function check(string $password, string $hashedPassword): bool
82
    {
83 2
        return $this->hash($password) === $hashedPassword;
84
    }
85
}
86