Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Hashing;
4
 
5
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
6
use RuntimeException;
7
 
8
class BcryptHasher extends AbstractHasher implements HasherContract
9
{
10
    /**
11
     * The default cost factor.
12
     *
13
     * @var int
14
     */
15
    protected $rounds = 10;
16
 
17
    /**
18
     * Indicates whether to perform an algorithm check.
19
     *
20
     * @var bool
21
     */
22
    protected $verifyAlgorithm = false;
23
 
24
    /**
25
     * Create a new hasher instance.
26
     *
27
     * @param  array  $options
28
     * @return void
29
     */
30
    public function __construct(array $options = [])
31
    {
32
        $this->rounds = $options['rounds'] ?? $this->rounds;
33
        $this->verifyAlgorithm = $options['verify'] ?? $this->verifyAlgorithm;
34
    }
35
 
36
    /**
37
     * Hash the given value.
38
     *
39
     * @param  string  $value
40
     * @param  array  $options
41
     * @return string
42
     *
43
     * @throws \RuntimeException
44
     */
45
    public function make($value, array $options = [])
46
    {
47
        $hash = password_hash($value, PASSWORD_BCRYPT, [
48
            'cost' => $this->cost($options),
49
        ]);
50
 
51
        if ($hash === false) {
52
            throw new RuntimeException('Bcrypt hashing not supported.');
53
        }
54
 
55
        return $hash;
56
    }
57
 
58
    /**
59
     * Check the given plain value against a hash.
60
     *
61
     * @param  string  $value
62
     * @param  string  $hashedValue
63
     * @param  array  $options
64
     * @return bool
65
     *
66
     * @throws \RuntimeException
67
     */
68
    public function check($value, $hashedValue, array $options = [])
69
    {
70
        if ($this->verifyAlgorithm && $this->info($hashedValue)['algoName'] !== 'bcrypt') {
71
            throw new RuntimeException('This password does not use the Bcrypt algorithm.');
72
        }
73
 
74
        return parent::check($value, $hashedValue, $options);
75
    }
76
 
77
    /**
78
     * Check if the given hash has been hashed using the given options.
79
     *
80
     * @param  string  $hashedValue
81
     * @param  array  $options
82
     * @return bool
83
     */
84
    public function needsRehash($hashedValue, array $options = [])
85
    {
86
        return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, [
87
            'cost' => $this->cost($options),
88
        ]);
89
    }
90
 
91
    /**
92
     * Set the default password work factor.
93
     *
94
     * @param  int  $rounds
95
     * @return $this
96
     */
97
    public function setRounds($rounds)
98
    {
99
        $this->rounds = (int) $rounds;
100
 
101
        return $this;
102
    }
103
 
104
    /**
105
     * Extract the cost value from the options array.
106
     *
107
     * @param  array  $options
108
     * @return int
109
     */
110
    protected function cost(array $options = [])
111
    {
112
        return $options['rounds'] ?? $this->rounds;
113
    }
114
}