Completed
Push — master ( 852cbb...117aa5 )
by diego
05:27
created

PasswordHashService::isValidMd5()   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
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace HDNET\Importr\Service;
3
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
4
use TYPO3\CMS\Saltedpasswords\Salt\SaltFactory;
5
6
/**
7
 * Class PasswordHashService.
8
 */
9
class PasswordHashService
10
{
11
    /**
12
     * This function takes a password as argument, salts it and returns the new password.
13
     *
14
     * @param string $password
15
     *
16
     * @return string
17
     */
18
    public function hash($password)
19
    {
20
        if (ExtensionManagementUtility::isLoaded('saltedpasswords')) {
21
            $salter = SaltFactory::getSaltingInstance(null, 'FE');
22
            $password = $salter->getHashedPassword($password);
23
24
            if ($this->isValidMd5($password)) {
25
                $password = 'M' . $password;
26
            }
27
        }
28
29
        return $password;
30
    }
31
32
    /**
33
     * This function checks if a password is in md5 format.
34
     *
35
     * @param string $md5
36
     *
37
     * @return int
38
     */
39
    protected function isValidMd5($md5 = '')
40
    {
41
        return preg_match('/^[a-f0-9]{32}$/', $md5);
42
    }
43
}