IpbPassword::verify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 5
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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
/**
12
 * Implements the password hashing algorithm of Invision Power Board (ipb).
13
 */
14 View Code Duplication
class IpbPassword implements IPassword {
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 3
    public function hash($password) {
20 3
        $salt = base64_encode(openssl_random_pseudo_bytes(12));
21 3
        return $this->hashRaw($password, $salt).'$'.$salt;
22
    }
23
24
    /**
25
     * Hashes a password with a given salt.
26
     *
27
     * @param string $password The password to hash.
28
     * @param string $salt The password salt.
29
     * @return string Returns the password hash.
30
     */
31 4
    protected function hashRaw($password, $salt) {
32 4
        $calc_hash = md5(md5($salt).md5($password));
33
34 4
        return $calc_hash;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function needsRehash($hash) {
41 1
        return false;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3
    public function verify($password, $hash) {
48 3
        list($stored_hash, $salt) = $this->splitHash($hash);
49 3
        $calc_hash = $this->hashRaw($password, $salt);
50 3
        return $calc_hash === $stored_hash;
51
    }
52
53
    /**
54
     * Split the hash into its calculated hash and salt.
55
     *
56
     * @param string $hash The hash to split.
57
     * @return array An array in the form [$hash, $salt].
58
     */
59 3
    protected function splitHash($hash) {
60 3
        if (strpos($hash, '$') === false) {
61 1
            return [false, false];
62
        } else {
63 2
            $parts = explode('$', $hash, 2);
64 2
            return $parts;
65
        }
66
    }
67
}
68