Skip to content

Commit 28baecf

Browse files
committed
New Config Type
1 parent da7f0e7 commit 28baecf

File tree

17 files changed

+208
-97
lines changed

17 files changed

+208
-97
lines changed

Aes.php

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@
4949
* @link https://github.com/ivantcholakov/gibberish-aes-php
5050
*
5151
* @version 1.2.0
52-
* @package Lib
52+
* @package Devbr
5353
*
5454
* @license The MIT License (MIT)
5555
* @link http://opensource.org/licenses/MIT
5656
*/
5757

58-
namespace Lib;
58+
namespace Devbr;
5959

60-
class Aes {
60+
class Aes
61+
{
6162

6263
// The default key size in bits.
6364
protected static $key_size = 256;
@@ -83,8 +84,12 @@ class Aes {
8384
// I am going in a future release to remove this feature permanently.
8485

8586
// This is a static class, instances are disabled.
86-
final private function __construct() {}
87-
final private function __clone() {}
87+
final private function __construct()
88+
{
89+
}
90+
final private function __clone()
91+
{
92+
}
8893

8994
/**
9095
* Crypt AES (256, 192, 128)
@@ -96,10 +101,12 @@ final private function __clone() {}
96101
* a binary key that is to be used for encryption.
97102
* @return mixed base64 encrypted string, FALSE on failure.
98103
*/
99-
public static function enc($string, $pass, $key_size = null)
100-
{
101-
if($key_size !== null) self::$key_size = $key_size;
102-
$key_size = self::$key_size;
104+
public static function enc($string, $pass, $key_size = null)
105+
{
106+
if ($key_size !== null) {
107+
self::$key_size = $key_size;
108+
}
109+
$key_size = self::$key_size;
103110

104111
// Set a random salt.
105112
$salt = self::random_pseudo_bytes(8);
@@ -114,7 +121,6 @@ public static function enc($string, $pass, $key_size = null)
114121
$salted_length = $key_length + $block_length;
115122

116123
while (self::strlen($salted) < $salted_length) {
117-
118124
$dx = md5($dx.$pass.$salt, true);
119125
$salted .= $dx;
120126
}
@@ -134,9 +140,11 @@ public static function enc($string, $pass, $key_size = null)
134140
* @param string $pass The secret pass-phrase that has been used for encryption.
135141
* @return mixed base64 decrypted string, FALSE on failure.
136142
*/
137-
public static function dec($string, $pass, $key_size = null)
138-
{
139-
if($key_size !== null) self::$key_size = $key_size;
143+
public static function dec($string, $pass, $key_size = null)
144+
{
145+
if ($key_size !== null) {
146+
self::$key_size = $key_size;
147+
}
140148
$key_size = self::$key_size;
141149

142150
// Lengths in bytes:
@@ -168,7 +176,6 @@ public static function dec($string, $pass, $key_size = null)
168176
$result = $md5_hash[0];
169177

170178
for ($i = 1; $i < $rounds; $i++) {
171-
172179
$md5_hash[$i] = md5($md5_hash[$i - 1].$data00, true);
173180
$result .= $md5_hash[$i];
174181
}
@@ -186,7 +193,8 @@ public static function dec($string, $pass, $key_size = null)
186193
* this method is just a getter of the current key size value.
187194
* @return integer Returns the old key size value.
188195
*/
189-
public static function size($newsize = null) {
196+
public static function size($newsize = null)
197+
{
190198

191199
$result = self::$key_size;
192200

@@ -215,29 +223,30 @@ public static function size($newsize = null) {
215223

216224
// Non-public methods ------------------------------------------------------
217225

218-
protected static function openssl_random_pseudo_bytes_exists() {
226+
protected static function openssl_random_pseudo_bytes_exists()
227+
{
219228

220229
if (!isset(self::$openssl_random_pseudo_bytes_exists)) {
221-
222230
self::$openssl_random_pseudo_bytes_exists = function_exists('openssl_random_pseudo_bytes') &&
223231
(version_compare(PHP_VERSION, '5.3.4') >= 0 || !self::is_windows());
224232
}
225233

226234
return self::$openssl_random_pseudo_bytes_exists;
227235
}
228236

229-
protected static function mcrypt_dev_urandom_exists() {
237+
protected static function mcrypt_dev_urandom_exists()
238+
{
230239

231240
if (!isset(self::$mcrypt_dev_urandom_exists)) {
232-
233241
self::$mcrypt_dev_urandom_exists = function_exists('mcrypt_create_iv') &&
234242
(version_compare(PHP_VERSION, '5.3.7') >= 0 || !self::is_windows());
235243
}
236244

237245
return self::$mcrypt_dev_urandom_exists;
238246
}
239247

240-
protected static function openssl_encrypt_exists() {
248+
protected static function openssl_encrypt_exists()
249+
{
241250

242251
if (!isset(self::$openssl_encrypt_exists)) {
243252
self::$openssl_encrypt_exists = function_exists('openssl_encrypt')
@@ -248,7 +257,8 @@ protected static function openssl_encrypt_exists() {
248257
return self::$openssl_encrypt_exists;
249258
}
250259

251-
protected static function openssl_decrypt_exists() {
260+
protected static function openssl_decrypt_exists()
261+
{
252262

253263
if (!isset(self::$openssl_decrypt_exists)) {
254264
self::$openssl_decrypt_exists = function_exists('openssl_decrypt')
@@ -259,7 +269,8 @@ protected static function openssl_decrypt_exists() {
259269
return self::$openssl_decrypt_exists;
260270
}
261271

262-
protected static function mcrypt_exists() {
272+
protected static function mcrypt_exists()
273+
{
263274

264275
if (!isset(self::$mcrypt_exists)) {
265276
self::$mcrypt_exists = function_exists('mcrypt_encrypt');
@@ -268,24 +279,26 @@ protected static function mcrypt_exists() {
268279
return self::$mcrypt_exists;
269280
}
270281

271-
protected static function openssl_cli_exists() {
282+
protected static function openssl_cli_exists()
283+
{
272284

273285
if (!isset(self::$openssl_cli_exists)) {
274-
275286
exec('openssl version', $output, $return);
276287
self::$openssl_cli_exists = $return == 0;
277288
}
278289

279290
return self::$openssl_cli_exists;
280291
}
281292

282-
protected static function is_windows() {
293+
protected static function is_windows()
294+
{
283295

284296
// Beware about 'Darwin'.
285297
return 0 === stripos(PHP_OS, 'win');
286298
}
287299

288-
protected static function mbstring_func_overload() {
300+
protected static function mbstring_func_overload()
301+
{
289302

290303
if (!isset(self::$mbstring_func_overload)) {
291304
self::$mbstring_func_overload = extension_loaded('mbstring') && ini_get('mbstring.func_overload');
@@ -294,32 +307,33 @@ protected static function mbstring_func_overload() {
294307
return self::$mbstring_func_overload;
295308
}
296309

297-
protected static function strlen($str) {
310+
protected static function strlen($str)
311+
{
298312

299313
return self::mbstring_func_overload() ? mb_strlen($str, '8bit') : strlen($str);
300314
}
301315

302-
protected static function substr($str, $start, $length = null) {
316+
protected static function substr($str, $start, $length = null)
317+
{
303318

304319
if (self::mbstring_func_overload()) {
305-
306320
// mb_substr($str, $start, null, '8bit') returns an empty string on PHP 5.3
307-
isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
321+
isset($length) or $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
308322

309323
return mb_substr($str, $start, $length, '8bit');
310324
}
311325

312326
return isset($length) ? substr($str, $start, $length) : substr($str, $start);
313327
}
314328

315-
protected static function random_pseudo_bytes($length) {
329+
protected static function random_pseudo_bytes($length)
330+
{
316331

317332
if (self::openssl_random_pseudo_bytes_exists()) {
318333
return openssl_random_pseudo_bytes($length);
319334
}
320335

321336
if (self::mcrypt_dev_urandom_exists()) {
322-
323337
$rnd = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
324338

325339
if ($rnd !== false) {
@@ -456,7 +470,8 @@ protected static function random_pseudo_bytes($length) {
456470
*/
457471
}
458472

459-
protected static function aes_cbc_encrypt($string, $key, $iv) {
473+
protected static function aes_cbc_encrypt($string, $key, $iv)
474+
{
460475

461476
$key_size = self::$key_size;
462477

@@ -465,14 +480,12 @@ protected static function aes_cbc_encrypt($string, $key, $iv) {
465480
}
466481

467482
if (self::mcrypt_exists()) {
468-
469483
// Info: http://www.chilkatsoft.com/p/php_aes.asp
470484
// http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
471485

472486
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
473487

474488
if (mcrypt_generic_init($cipher, $key, $iv) != -1) {
475-
476489
$encrypted = mcrypt_generic($cipher, self::pkcs7_pad($string));
477490
mcrypt_generic_deinit($cipher);
478491
mcrypt_module_close($cipher);
@@ -484,7 +497,6 @@ protected static function aes_cbc_encrypt($string, $key, $iv) {
484497
}
485498

486499
if (self::openssl_cli_exists()) {
487-
488500
$cmd = 'echo '.self::escapeshellarg($string).' | openssl enc -e -a -A -aes-'.$key_size.'-cbc -K '.self::strtohex($key).' -iv '.self::strtohex($iv);
489501

490502
exec($cmd, $output, $return);
@@ -501,7 +513,8 @@ protected static function aes_cbc_encrypt($string, $key, $iv) {
501513
return false;
502514
}
503515

504-
protected static function aes_cbc_decrypt($crypted, $key, $iv) {
516+
protected static function aes_cbc_decrypt($crypted, $key, $iv)
517+
{
505518

506519
$key_size = self::$key_size;
507520

@@ -510,11 +523,9 @@ protected static function aes_cbc_decrypt($crypted, $key, $iv) {
510523
}
511524

512525
if (self::mcrypt_exists()) {
513-
514526
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
515527

516528
if (mcrypt_generic_init($cipher, $key, $iv) != -1) {
517-
518529
$decrypted = mdecrypt_generic($cipher, $crypted);
519530
mcrypt_generic_deinit($cipher);
520531
mcrypt_module_close($cipher);
@@ -526,7 +537,6 @@ protected static function aes_cbc_decrypt($crypted, $key, $iv) {
526537
}
527538

528539
if (self::openssl_cli_exists()) {
529-
530540
$string = base64_encode($crypted);
531541

532542
$cmd = 'echo '.self::escapeshellarg($string).' | openssl enc -d -a -A -aes-'.$key_size.'-cbc -K '.self::strtohex($key).' -iv '.self::strtohex($iv);
@@ -547,7 +557,8 @@ protected static function aes_cbc_decrypt($crypted, $key, $iv) {
547557

548558
// See http://www.php.net/manual/en/function.mcrypt-decrypt.php#105985
549559

550-
protected static function pkcs7_pad($string) {
560+
protected static function pkcs7_pad($string)
561+
{
551562

552563
// 128 bits: $block_length = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
553564
$block_length = 16;
@@ -556,19 +567,18 @@ protected static function pkcs7_pad($string) {
556567
return $string.str_repeat(chr($pad), $pad);
557568
}
558569

559-
protected static function remove_pkcs7_pad($string) {
570+
protected static function remove_pkcs7_pad($string)
571+
{
560572

561573
// 128 bits: $block_length = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
562574
$block_length = 16;
563575
$len = self::strlen($string);
564576
$pad = ord($string[$len - 1]);
565577

566578
if ($pad > 0 && $pad <= $block_length) {
567-
568579
$valid_pad = true;
569580

570581
for ($i = 1; $i <= $pad; $i++) {
571-
572582
if (ord($string[$len - $i]) != $pad) {
573583
$valid_pad = false;
574584
break;
@@ -583,7 +593,8 @@ protected static function remove_pkcs7_pad($string) {
583593
return $string;
584594
}
585595

586-
protected static function strtohex($string) {
596+
protected static function strtohex($string)
597+
{
587598

588599
$result = '';
589600

@@ -594,10 +605,10 @@ protected static function strtohex($string) {
594605
return $result;
595606
}
596607

597-
protected static function escapeshellarg($arg) {
608+
protected static function escapeshellarg($arg)
609+
{
598610

599611
if (self::is_windows()) {
600-
601612
// See http://stackoverflow.com/questions/6427732/how-can-i-escape-an-arbitrary-string-for-use-as-a-command-line-argument-in-windo
602613

603614
// Sequence of backslashes followed by a double quote:
@@ -623,5 +634,4 @@ protected static function escapeshellarg($arg) {
623634
// See http://markushedlund.com/dev-tech/php-escapeshellarg-with-unicodeutf-8-support
624635
return "'" . str_replace("'", "'\\''", $arg) . "'";
625636
}
626-
627637
}

App.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Lib\App
3+
* Devbr\App
44
* PHP version 7
55
*
66
* @category Tools
@@ -12,10 +12,10 @@
1212
* @link http://paulorocha.tk/devbr
1313
*/
1414

15-
namespace Lib;
15+
namespace Devbr;
1616

17-
use Lib;
18-
use Config\Lib\Resource;
17+
use Devbr;
18+
use Config\Devbr\Resource;
1919

2020
/**
2121
* App Class

0 commit comments

Comments
 (0)