Projekt

Obecné

Profil

Stáhnout (3.03 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Encryption;
4

    
5
use RuntimeException;
6
use Illuminate\Contracts\Encryption\DecryptException;
7
use Illuminate\Contracts\Encryption\EncryptException;
8
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
9

    
10
class Encrypter extends BaseEncrypter implements EncrypterContract
11
{
12
    /**
13
     * The algorithm used for encryption.
14
     *
15
     * @var string
16
     */
17
    protected $cipher;
18

    
19
    /**
20
     * Create a new encrypter instance.
21
     *
22
     * @param  string  $key
23
     * @param  string  $cipher
24
     * @return void
25
     *
26
     * @throws \RuntimeException
27
     */
28
    public function __construct($key, $cipher = 'AES-128-CBC')
29
    {
30
        $key = (string) $key;
31

    
32
        if (static::supported($key, $cipher)) {
33
            $this->key = $key;
34
            $this->cipher = $cipher;
35
        } else {
36
            throw new RuntimeException('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');
37
        }
38
    }
39

    
40
    /**
41
     * Determine if the given key and cipher combination is valid.
42
     *
43
     * @param  string  $key
44
     * @param  string  $cipher
45
     * @return bool
46
     */
47
    public static function supported($key, $cipher)
48
    {
49
        $length = mb_strlen($key, '8bit');
50

    
51
        return ($cipher === 'AES-128-CBC' && $length === 16) || ($cipher === 'AES-256-CBC' && $length === 32);
52
    }
53

    
54
    /**
55
     * Encrypt the given value.
56
     *
57
     * @param  string  $value
58
     * @return string
59
     *
60
     * @throws \Illuminate\Contracts\Encryption\EncryptException
61
     */
62
    public function encrypt($value)
63
    {
64
        $iv = random_bytes($this->getIvSize());
65

    
66
        $value = \openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv);
67

    
68
        if ($value === false) {
69
            throw new EncryptException('Could not encrypt the data.');
70
        }
71

    
72
        // Once we have the encrypted value we will go ahead base64_encode the input
73
        // vector and create the MAC for the encrypted value so we can verify its
74
        // authenticity. Then, we'll JSON encode the data in a "payload" array.
75
        $mac = $this->hash($iv = base64_encode($iv), $value);
76

    
77
        $json = json_encode(compact('iv', 'value', 'mac'));
78

    
79
        if (! is_string($json)) {
80
            throw new EncryptException('Could not encrypt the data.');
81
        }
82

    
83
        return base64_encode($json);
84
    }
85

    
86
    /**
87
     * Decrypt the given value.
88
     *
89
     * @param  string  $payload
90
     * @return string
91
     *
92
     * @throws \Illuminate\Contracts\Encryption\DecryptException
93
     */
94
    public function decrypt($payload)
95
    {
96
        $payload = $this->getJsonPayload($payload);
97

    
98
        $iv = base64_decode($payload['iv']);
99

    
100
        $decrypted = \openssl_decrypt($payload['value'], $this->cipher, $this->key, 0, $iv);
101

    
102
        if ($decrypted === false) {
103
            throw new DecryptException('Could not decrypt the data.');
104
        }
105

    
106
        return unserialize($decrypted);
107
    }
108

    
109
    /**
110
     * Get the IV size for the cipher.
111
     *
112
     * @return int
113
     */
114
    protected function getIvSize()
115
    {
116
        return 16;
117
    }
118
}
(2-2/5)