Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

PasswordEncoder::createSalt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Security\Core\Encoder;
26
27
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
28
29
class PasswordEncoder implements PasswordEncoderInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
30
{
31
32
    /* @var $config array */
33
    public $config;
34
35 1068
    public function __construct(array $config)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
36
    {
37 1068
        $this->config = $config;
38
    }
39
40
    /**
41
     * Encodes the raw password.
42
     *
43
     * @param string $raw  The password to encode
44
     * @param string $salt The salt
45
     *
46
     * @return string The encoded password
47
     */
48 426
    public function encodePassword($raw, $salt)
49
    {
50 426
        if ($salt == '') {
51
            $salt = $this->config['auth_magic'];
52
        }
53 426
        if ($this->config['auth_type'] == 'PLAIN') {
54
            $res = $raw;
55
        } else {
56 426
            $res = hash_hmac($this->config['password_hash_algos'], $raw . ':' . $this->config['auth_magic'], $salt);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
57
        }
58
59 426
        return $res;
60
    }
61
62
    /**
63
     * Checks a raw password against an encoded password.
64
     *
65
     * @param string $encoded An encoded password
66
     * @param string $raw     A raw password
67
     * @param string $salt    The salt
68
     *
69
     * @return bool true if the password is valid, false otherwise
70
     */
71 1
    public function isPasswordValid($encoded, $raw, $salt)
72
    {
73 1
        if ($encoded == '') {
74
            return false;
75
        }
76
77 1
        if ($this->config['auth_type'] == 'PLAIN') {
78
            if ($raw === $encoded) {
79
                return true;
80
            }
81
        } else {
82
            // 旧バージョン(2.11未満)からの移行を考慮
83 1
            if (empty($salt)) {
84
                $hash = sha1($raw . ':' . $this->config['auth_magic']);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
85
            } else {
86 1
                $hash = $this->encodePassword($raw, $salt);
87
            }
88
89 1
            if ($hash === $encoded) {
90 1
                return true;
91
            }
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * saltを生成する.
99
     *
100
     * @param int $length
101
     * @return string
102
     */
103 235
    public function createSalt($length = 5)
104
    {
105 235
        return bin2hex(openssl_random_pseudo_bytes($length));
106
    }
107
}
108