Skip to content

Commit

Permalink
Minor cleanup (phpcs, phpmd etc) Now testing on multiple php versions
Browse files Browse the repository at this point in the history
  • Loading branch information
annejan committed Apr 18, 2018
1 parent 9e04e38 commit 1917e43
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage
vendor
.DS_Store
.idea
composer.lock
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2

before_script:
- composer self-update
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ composer require noprotocol/php-mysql-aes-crypt
```json
{
"require": {
"noprotocol/php-mysql-aes-crypt": "^1.0.0"
"noprotocol/php-mysql-aes-crypt": "^2.0.0"
}
}
```
Expand All @@ -26,6 +26,7 @@ use NoProtocol\Encryption\MySQL\AES\Crypter;
```

<a name="install-nocomposer"/>

### Without Composer

Please use [Composer](http://getcomposer.org/). If you need to install manually, download [Crypter.php](https://github.com/noprotocol/php-mysql-aes-crypt/src/NoProtocol/Encryption/MySQL/AES/Crypter.php) from the repository and save the file into your project path.
Expand Down
32 changes: 16 additions & 16 deletions src/Crypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
/**
* Encrypt/decrypt data to a format compatible with MySQL aes_encrypt() & aes_decrypt() functions.
*
* @package NoProtocol\Encryption\MySQL\AES
* @author Bob Fanger <[email protected]>
* @author Anne Jan Brouwer <[email protected]>
* @author Govert Verschuur <[email protected]>
* @author Renan Martins Pimentel <[email protected]>
* @copyright 2016 NoProtocol
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
* @version 2.0.0
* @link http://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/
* @package NoProtocol\Encryption\MySQL\AES
* @author Bob Fanger <[email protected]>
* @author Anne Jan Brouwer <[email protected]>
* @author Govert Verschuur <[email protected]>
* @author Renan Martins Pimentel <[email protected]>
* @copyright 2016 NoProtocol
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
* @version 2.0.0
* @link http://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/
*/

namespace NoProtocol\Encryption\MySQL\AES;

class Crypter
class Crypter
{
protected $key;

Expand All @@ -27,17 +27,17 @@ function __construct($seed)
/**
* Encrypts the data
*
* @since 2.0
* @since 2.0
* @param string $data A string of data to encrypt.
* @return (binary) string The encrypted data
*/
public function encrypt($data)
{
$chiperIvLength = openssl_cipher_iv_length('AES-128-ECB');
$iv = openssl_random_pseudo_bytes($chiperIvLength);
$pad_value = 16 - (strlen($data) % 16);
$padValue = 16 - (strlen($data) % 16);
return openssl_encrypt(
str_pad($data, (16 * (floor(strlen($data) / 16) + 1)), chr($pad_value)),
str_pad($data, (16 * (floor(strlen($data) / 16) + 1)), chr($padValue)),
'AES-128-ECB',
$this->key,
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,
Expand All @@ -48,7 +48,7 @@ public function encrypt($data)
/**
* Decrypts the data.
*
* @since 2.0
* @since 2.0
* @param string $data A (binary) string of encrypted data
* @return string Decrypted data
*/
Expand All @@ -61,13 +61,13 @@ public function decrypt($data)
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING
);

return rtrim($data,"\x00..\x10");
return rtrim($data, "\x00..\x10");
}

/**
* Create and set the key used for encryption.
*
* @since 2.0
* @since 2.0
* @param string $seed The seed used to create the key.
* @return (binary) string the key to use in the encryption process.
*/
Expand Down
31 changes: 16 additions & 15 deletions tests/CrypterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
use NoProtocol\Encryption\MySQL\AES\Crypter;
use PHPUnit\Framework\TestCase;

class AESEncryptTest extends TestCase {
class AESEncryptTest extends TestCase
{

public function setUp()
{
parent::setUp();
}
public function setUp()
{
parent::setUp();
}

public function testItEncrypts()
{
$crypter = new Crypter('mysecretseedingkey');
$result = $crypter->encrypt('foobar');
$this->assertEquals('iWSjHbqpoNOPS6p1FsyyZw==', base64_encode($result));
public function testItEncrypts()
{
$crypter = new Crypter('mysecretseedingkey');
$result = $crypter->encrypt('foobar');
$this->assertEquals('iWSjHbqpoNOPS6p1FsyyZw==', base64_encode($result));
}

public function testItDecrypts()
{
$crypter = new Crypter('mysecretseedingkey');
$result = $crypter->decrypt(base64_decode('iWSjHbqpoNOPS6p1FsyyZw=='));
$this->assertEquals('foobar', $result);
public function testItDecrypts()
{
$crypter = new Crypter('mysecretseedingkey');
$result = $crypter->decrypt(base64_decode('iWSjHbqpoNOPS6p1FsyyZw=='));
$this->assertEquals('foobar', $result);
}
}

0 comments on commit 1917e43

Please sign in to comment.