Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
/*
4
 * This file is part of the MesCryptoBundle package.
5
 *
6
 * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mes\Security\CryptoBundle;
13
14
use Mes\Security\CryptoBundle\KeyGenerator\KeyGeneratorInterface;
15
use Mes\Security\CryptoBundle\KeyStorage\KeyStorageInterface;
16
use Mes\Security\CryptoBundle\Model\KeyInterface;
17
18
/**
19
 * Class KeyManager.
20
 */
21
final class KeyManager implements KeyManagerInterface
22
{
23
    /**
24
     * @var KeyStorageInterface
25
     */
26
    protected $keyStorage;
27
28
    /**
29
     * @var KeyGeneratorInterface
30
     */
31
    protected $keyGenerator;
32
33
    /**
34
     * @var string
35
     */
36
    protected $secret;
37
38
    /**
39
     * KeyManager constructor.
40
     *
41
     * @param KeyStorageInterface   $keyStorage
42
     * @param KeyGeneratorInterface $keyGenerator
43
     */
44 6
    public function __construct(KeyStorageInterface $keyStorage, KeyGeneratorInterface $keyGenerator)
45
    {
46 6
        $this->keyStorage = $keyStorage;
47 6
        $this->keyGenerator = $keyGenerator;
48 6
        $this->setSecret(null);
49 6
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function generate($secret = null)
55
    {
56 2
        return $this->keyGenerator->generate($secret);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function generateFromAscii($key_encoded, $secret = null)
63
    {
64 1
        return $this->keyGenerator->generateFromAscii($key_encoded, $secret);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    public function getKey()
71
    {
72 2
        if (null === $key = $this->keyStorage->getKey()) {
73 1
            $this->keyStorage->setKey($key = $this->generate($this->getSecret()));
74
        }
75
76 2
        return $key;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function setKey(KeyInterface $key)
83
    {
84 1
        $this->keyStorage->setKey($key);
85 1
    }
86
87
    /**
88
     * Gets the secret string used to make the Key for encryption.
89
     *
90
     * @return string The secret string used to make the Key for encryption
91
     */
92 2
    public function getSecret()
93
    {
94 2
        return $this->secret;
95
    }
96
97
    /**
98
     * Sets the secret string used to make the Key for encryption.
99
     *
100
     * @param string $secret The secret string used to make the Key for encryption
101
     */
102 6
    public function setSecret($secret)
103
    {
104 6
        $this->secret = $secret;
105 6
    }
106
}
107