Completed
Pull Request — newinternal (#285)
by Simon
03:26
created

AuthUtility::encryptPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca;
10
11
class AuthUtility
12
{
13
    /**
14
     * Test the specified data against the specified credentials
15
     *
16
     * @param string $password
17
     * @param string $credentials
18
     *
19
     * @return bool
20
     */
21
    public static function testCredentials($password, $credentials)
22
    {
23
        global $minimumPasswordVersion;
24
25
        if (substr($credentials, 0, 1) != ":") {
26
            return false;
27
        }
28
29
        // determine password version
30
        $data = explode(':', substr($credentials, 1));
31
32
        // call the encryptVersion function for the version that this password actually is.
33
        // syntax: :1:SALT:HASH
34
        // syntax: :2:x:HASH
35
36
        // check the version is one of the allowed ones:
37
        if ($minimumPasswordVersion > $data[0]) {
38
            return false;
39
        }
40
41
        if ($data[0] == 1) {
42
            return $credentials == self::encryptVersion1($password, $data[1]);
43
        }
44
45
        if ($data[0] == 2) {
46
            return self::verifyVersion2($password, $data[2]);
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * @param string $credentials
54
     *
55
     * @return bool
56
     */
57
    public static function isCredentialVersionLatest($credentials)
58
    {
59
        return substr($credentials, 0, 3) === ":2:";
60
    }
61
62
    /**
63
     * Encrypts a user's password with the latest version of the hash algorithm
64
     *
65
     * @param string $password
66
     *
67
     * @return string
68
     */
69
    public static function encryptPassword($password)
70
    {
71
        return self::encryptVersion2($password);
72
    }
73
74
    /**
75
     * @param string $password
76
     * @param string $salt
77
     *
78
     * @return string
79
     */
80
    private static function encryptVersion1($password, $salt)
81
    {
82
        return ':1:' . $salt . ':' . md5($salt . '-' . md5($password));
83
    }
84
85
    /**
86
     * @param string $password
87
     *
88
     * @return string
89
     */
90
    private static function encryptVersion2($password)
91
    {
92
        return ':2:x:' . password_hash($password, PASSWORD_BCRYPT);
93
    }
94
95
    /**
96
     * @param string $password
97
     * @param string $hash
98
     *
99
     * @return bool
100
     */
101
    private static function verifyVersion2($password, $hash)
102
    {
103
        return password_verify($password, $hash);
104
    }
105
}
106