From 6282d5de6eb244847532e4dbbd0a44c4af66ed34 Mon Sep 17 00:00:00 2001 From: Alasjo Date: Mon, 20 Jul 2015 21:49:57 +0200 Subject: [PATCH 1/3] If PHP <= 5.4.0, use an alternative hex2bin Signed-off-by: Alasjo --- AESCryptFileLib.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/AESCryptFileLib.php b/AESCryptFileLib.php index cf2b1e3..9495042 100644 --- a/AESCryptFileLib.php +++ b/AESCryptFileLib.php @@ -544,7 +544,7 @@ private function getBinaryExtensionData($ext_data) private function createKeyUsingIVAndPassphrase($iv, $passphrase) { //Start with the IV padded to 32 bytes - $aes_key = str_pad($iv, 32, hex2bin("00")); + $aes_key = str_pad($iv, 32, self::hex2bin("00")); $iterations = 8192; for($i=0; $i<$iterations; $i++) { @@ -614,6 +614,22 @@ public static function bin_substr($string, $start, $length = NULL) { return substr($string, $start, $length); } } + + //hex2bin wasn't introduced until PHP 5.4.0. If not present, use an alternative + //written by jannik: http://php.net/manual/en/function.hex2bin.php#113057 + public static function hex2bin($string) { + if (function_exists('hex2bin')) { + return hex2bin($string); + } else { + $sbin = ""; + $len = strlen( $str ); + for ( $i = 0; $i < $len; $i += 2 ) { + $sbin .= pack( "H*", substr( $str, $i, 2 ) ); + } + + return $sbin; + } + } } From 78192f0c5fcfd440b8d04b1c52de6f9eff49aee3 Mon Sep 17 00:00:00 2001 From: Alasjo Date: Tue, 21 Jul 2015 10:24:40 +0200 Subject: [PATCH 2/3] mb_substr treats null $length as 0, fix Signed-off-by: Alasjo --- AESCryptFileLib.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/AESCryptFileLib.php b/AESCryptFileLib.php index 9495042..1f53e5d 100644 --- a/AESCryptFileLib.php +++ b/AESCryptFileLib.php @@ -609,6 +609,11 @@ public static function bin_strpos($haystack, $needle, $offset = 0) { public static function bin_substr($string, $start, $length = NULL) { if (function_exists('mb_substr')) { + if (is_null($length)) { + //Passing $length=NULL to mb_substring will treat it as 0 + //http://php.net/manual/en/function.mb-substr.php#77515 + $length = mb_strlen($string, '8bit'); + } return mb_substr($string, $start, $length, '8bit'); } else { return substr($string, $start, $length); From 50c9e752be0e17fa137e9a4f2a897a54867aaeb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Alasj=C3=B6?= Date: Thu, 29 Dec 2016 15:41:58 +0100 Subject: [PATCH 3/3] Fix variable typo Changed from `$str` to `$string` --- AESCryptFileLib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AESCryptFileLib.php b/AESCryptFileLib.php index 1f53e5d..0a656d7 100644 --- a/AESCryptFileLib.php +++ b/AESCryptFileLib.php @@ -627,9 +627,9 @@ public static function hex2bin($string) { return hex2bin($string); } else { $sbin = ""; - $len = strlen( $str ); + $len = strlen( $string ); for ( $i = 0; $i < $len; $i += 2 ) { - $sbin .= pack( "H*", substr( $str, $i, 2 ) ); + $sbin .= pack( "H*", substr( $string, $i, 2 ) ); } return $sbin;