JoomlaPassword::hash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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