Skip to content

Commit 0cb96b2

Browse files
Fixed the encrypter
1 parent 14e83fd commit 0cb96b2

2 files changed

Lines changed: 60 additions & 27 deletions

File tree

src/Illuminate/Encryption/Encrypter.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Encrypter {
4141
*/
4242
public function __construct($key)
4343
{
44-
$this->setKey($key);
44+
$this->key = (string) $key;
4545
}
4646

4747
/**
@@ -52,6 +52,8 @@ public function __construct($key)
5252
*/
5353
public function encrypt($value)
5454
{
55+
$this->checkKey();
56+
5557
$iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());
5658

5759
$value = base64_encode($this->padAndMcrypt($value, $iv));
@@ -86,6 +88,8 @@ protected function padAndMcrypt($value, $iv)
8688
*/
8789
public function decrypt($payload)
8890
{
91+
$this->checkKey();
92+
8993
$payload = $this->getJsonPayload($payload);
9094

9195
// We'll go ahead and remove the PKCS7 padding from the encrypted value before
@@ -263,27 +267,10 @@ protected function getRandomizer()
263267
*
264268
* @param string $key
265269
* @return void
266-
*
267-
* @throws \Illuminate\Encryption\InvalidKeyException
268270
*/
269271
public function setKey($key)
270272
{
271-
if ( ! is_string($key))
272-
{
273-
throw new InvalidKeyException('The encryption key must be a string.');
274-
}
275-
276-
if ($key === '')
277-
{
278-
throw new InvalidKeyException('The encryption key must be not be empty.');
279-
}
280-
281-
if ($key === 'YourSecretKey!!!')
282-
{
283-
throw new InvalidKeyException('The encryption key must be a random string.');
284-
}
285-
286-
$this->key = $key;
273+
$this->key = (string) $key;
287274
}
288275

289276
/**
@@ -322,4 +309,24 @@ protected function updateBlockSize()
322309
$this->block = mcrypt_get_iv_size($this->cipher, $this->mode);
323310
}
324311

312+
/**
313+
* Check the current key is usable to perform cryptographic operations.
314+
*
315+
* @return void
316+
*
317+
* @throws \Illuminate\Encryption\InvalidKeyException
318+
*/
319+
protected function checkKey()
320+
{
321+
if ($this->key === '')
322+
{
323+
throw new InvalidKeyException('The encryption key must not be empty.');
324+
}
325+
326+
if ($this->key === 'YourSecretKey!!!')
327+
{
328+
throw new InvalidKeyException('The encryption key must be a random string.');
329+
}
330+
}
331+
325332
}

tests/Encryption/EncrypterTest.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,59 @@ public function testExceptionThrownWhenPayloadIsInvalid()
3535
}
3636

3737

38+
public function testCanStillBeConstructedWithInvalidKeys()
39+
{
40+
$e = new Encrypter(''); // should not throw an exception
41+
42+
$e = new Encrypter('YourSecretKey!!!'); // should not throw an exception
43+
}
44+
45+
3846
/**
3947
* @expectedException Illuminate\Encryption\InvalidKeyException
40-
* @expectedExceptionMessage The encryption key must be a string.
48+
* @expectedExceptionMessage The encryption key must not be empty.
4149
*/
42-
public function testConstuctWithNonString()
50+
public function testEncryptWithEmptyStringAsKey()
4351
{
44-
return new Encrypter(123);
52+
$e = new Encrypter('');
53+
54+
$e->encrypt('bar'); // throw the exception now that we tried to use the encrypter
4555
}
4656

4757

4858
/**
4959
* @expectedException Illuminate\Encryption\InvalidKeyException
50-
* @expectedExceptionMessage The encryption key must be not be empty.
60+
* @expectedExceptionMessage The encryption key must not be empty.
5161
*/
52-
public function testConstuctWithEmptyString()
62+
public function testDecryptWithEmptyStringAsKey()
5363
{
54-
return new Encrypter('');
64+
$e = new Encrypter('');
65+
66+
$e->decrypt('bar'); // throw the exception now that we tried to use the encrypter
5567
}
5668

5769

5870
/**
5971
* @expectedException Illuminate\Encryption\InvalidKeyException
6072
* @expectedExceptionMessage The encryption key must be a random string.
6173
*/
62-
public function testConstuctWithDefaultString()
74+
public function testEncryptWithDefaultStringAsKey()
6375
{
64-
return new Encrypter('YourSecretKey!!!');
76+
$e = new Encrypter('YourSecretKey!!!');
77+
78+
$e->encrypt('bar'); // throw the exception now that we tried to use the encrypter
79+
}
80+
81+
82+
/**
83+
* @expectedException Illuminate\Encryption\InvalidKeyException
84+
* @expectedExceptionMessage The encryption key must be a random string.
85+
*/
86+
public function testDecryptWithDefaultStringAsKey()
87+
{
88+
$e = new Encrypter('YourSecretKey!!!');
89+
90+
$e->decrypt('bar'); // throw the exception now that we tried to use the encrypter
6591
}
6692

6793

0 commit comments

Comments
 (0)