Hasher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Crypt;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Crypt\Contracts\HasherInterface;
22
use function assert;
23
use function password_hash;
24
use function password_verify;
25
26
/**
27
 * @package Limoncello\Crypt
28
 */
29
class Hasher implements HasherInterface
30
{
31
    /**
32
     * @var int
33
     */
34
    private $algorithm;
35
36
    /**
37
     * @var array
38
     */
39
    private $options;
40
41
    /**
42
     * @param int $algorithm
43
     * @param int $cost
44
     */
45 2
    public function __construct(int $algorithm = PASSWORD_DEFAULT, int $cost = 10)
46
    {
47 2
        assert($cost > 0);
48
49 2
        $this->algorithm = $algorithm;
50 2
        $this->options   = [
51 2
            'cost' => $cost,
52
        ];
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 1
    public function hash(string $password): string
59
    {
60 1
        $hash = password_hash($password, $this->algorithm, $this->options);
61
62 1
        return $hash;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function verify(string $password, string $hash): bool
69
    {
70 1
        $result = password_verify($password, $hash);
71
72 1
        return $result;
73
    }
74
}
75