IpbPassword   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 54
loc 54
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 4 4 1
A hashRaw() 5 5 1
A needsRehash() 3 3 1
A verify() 5 5 1
A splitHash() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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