Completed
Push — master ( c2bd55...6b7717 )
by Todd
09:04
created

VanillaPassword   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.1%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 23
c 4
b 0
f 2
lcom 1
cbo 1
dl 0
loc 65
ccs 27
cts 31
cp 0.871
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hash() 0 7 3
B needsRehash() 0 11 10
D verify() 0 27 9
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Password;
9
10
/**
11
 * Implements the default Vanilla password algorithm.
12
 */
13
class VanillaPassword extends PhpassPassword {
14
    /**
15
     * Initialize an instance of the {@link VanillaPassword} class.
16
     */
17 2
    public function __construct() {
18 2
        parent::__construct(PhpassPassword::HASH_BEST);
19 2
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 5
    public function hash($password) {
25 5
        if ($this->hashMethod === static::HASH_BEST && function_exists('password_hash')) {
26
            return password_hash($password, PASSWORD_DEFAULT);
27
        } else {
28 5
            return parent::hash($password);
29
        }
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function needsRehash($hash) {
36 2
        if ($this->hashMethod === static::HASH_BEST &&  function_exists('password_needs_rehash')) {
37
            return password_needs_rehash($hash, PASSWORD_DEFAULT);
38 2
        } elseif (($this->hashMethod & static::HASH_BLOWFISH) && CRYPT_BLOWFISH === 1) {
39 2
            return !(preg_match('`^\$(2[axy]|[56])\$`', $hash) && strlen($hash) === 60);
40 1
        } elseif (($this->hashMethod & static::HASH_EXTDES) && CRYPT_EXT_DES === 1) {
41 1
            return !(preg_match('`^_[./0-9A-Za-z]{8}`', $hash) && strlen($hash) === 20);
42
        } else {
43 1
            return !(preg_match('`^\$([PH])\$`', $hash) && strlen($hash) === 34);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 5
    public function verify($password, $hash) {
51 5
        if (!$hash) {
52 2
            return false;
53
        }
54
55 4
        if ($this->hashMethod === static::HASH_BEST &&
56 4
            function_exists('password_verify') &&
57 4
            password_verify((string)$password, (string)$hash)) {
58
59
            return true;
60
        }
61
62 4
        if (!$hash) {
63
            return false;
64
        }
65
66
        // Check against a php pass style password.
67 4
        if (in_array(substr($hash, 0, 1), ['_', '$'])) {
68 4
            return parent::verify($password, $hash);
69 1
        } elseif (md5($password) === $hash) {
70 1
            return true;
71 1
        } elseif ($password === $hash) {
72 1
            return true;
73
        }
74 1
        return false;
75
76
    }
77
}
78