Browse Source

Beginning modernization effort

Curtis Farnham 7 years ago
parent
commit
13c59ffb6a
5 changed files with 35 additions and 30 deletions
  1. 1 1
      .travis.yml
  2. 7 6
      composer.json
  3. 20 5
      lib/RNCryptor/Cryptor.php
  4. 5 10
      lib/RNCryptor/Decryptor.php
  5. 2 8
      lib/RNCryptor/Encryptor.php

+ 1 - 1
.travis.yml

@@ -1,8 +1,8 @@
 language: php
 php:
-  - 5.5
   - 5.6
   - 7.0
+  - 7.1
 before_script:
   - composer install
 script: vendor/bin/phpunit

+ 7 - 6
composer.json

@@ -20,15 +20,16 @@
         }
     ],
     "require": {
-        "php": ">=5.5.0",
-        "ext-mcrypt": "*",
-		"sarciszewski/php-future": "0.4.*"
+        "php": ">=5.6",
+        "ext-openssl": "*"
     },
     "require-dev": {
-		"rncryptor/spec": "3.0.1",
-		"phpunit/phpunit": "~4.8"
+		"rncryptor/spec": "~4.0",
+		"phpunit/phpunit": "~6.0"
     },
     "autoload": {
-        "psr-0": {"RNCryptor": "lib/"}
+        "psr-0": {
+            "RNCryptor": "lib/"
+        }
     }
 }

+ 20 - 5
lib/RNCryptor/Cryptor.php

@@ -8,8 +8,8 @@ class Cryptor {
 	protected $_settings;
 
 	public function __construct() {
-		if (!extension_loaded('mcrypt')) {
-			throw new \Exception('The mcrypt extension is missing.');
+		if (!extension_loaded('openssl')) {
+			throw new \Exception('RNCryptor requires the openssl extension.');
 		}
 	}
 
@@ -17,7 +17,7 @@ class Cryptor {
 
 		$settings = new \stdClass();
 
-		$settings->algorithm = MCRYPT_RIJNDAEL_128;
+		$settings->algorithm = 'aes-256-';
 		$settings->saltLength = 8;
 		$settings->ivLength = 16;
 
@@ -73,6 +73,22 @@ class Cryptor {
 		$this->_settings = $settings;
 	}
 
+    protected function _decrypt_internal($key, $payload, $mode, $iv = null) {
+
+        if ($iv == null) {
+            $iv = "";
+        }
+        return openssl_decrypt($payload, $this->_settings->algorithm.$mode, $key, OPENSSL_RAW_DATA, $iv);
+    }
+
+    protected function _encrypt_internal($key, $payload, $mode, $iv = null) {
+
+        if ($iv == null) {
+            $iv = "";
+        }
+        return openssl_encrypt($payload, $this->_settings->algorithm.$mode, $key, OPENSSL_RAW_DATA, $iv);
+    }
+
 	/**
 	 * Encrypt or decrypt using AES CTR Little Endian mode
 	 */
@@ -89,7 +105,7 @@ class Cryptor {
 			$iv[0] = chr(ord($iv[0]) + 1);
 		}
 
-		return $payload ^ mcrypt_encrypt($this->_settings->algorithm, $key, $counter, 'ecb');
+        return $payload ^ $this->_encrypt_internal($key, $counter, 'ecb');
 	}
 
 	protected function _generateHmac(\stdClass $components, $hmacKey) {
@@ -146,5 +162,4 @@ class Cryptor {
 		}
 		return mcrypt_create_iv($blockSize, $randomSource);
 	}
-
 }

+ 5 - 10
lib/RNCryptor/Decryptor.php

@@ -33,8 +33,7 @@ class Decryptor extends Cryptor {
 				break;
 
 			case 'cbc':
-				$paddedPlaintext = mcrypt_decrypt($this->_settings->algorithm, $key, $components->ciphertext, 'cbc', $components->headers->iv);
-				$plaintext = $this->_stripPKCS7Padding($paddedPlaintext);
+                $plaintext = $this->_decrypt_internal($key, $components->ciphertext, 'cbc', $components->headers->iv);
 				break;
 		}
 
@@ -117,13 +116,9 @@ class Decryptor extends Cryptor {
 		return substr($plaintext, 0, strlen($plaintext) - $padLength);
 	}
 
-	private function _hmacIsValid($components, $password, $isPasswordBased = true) {
-		$hmacKey = null;
-		if($isPasswordBased)
-			$hmacKey = $this->_generateKey($components->headers->hmacSalt, $password);
-		else
-			$hmacKey = $password;
-		return hash_equals($components->hmac, $this->_generateHmac($components, $hmacKey));
-	}
+	private function _hmacIsValid($components, $password) {
+        $hmacKey = $this->_generateKey($components->headers->hmacSalt, $password);
 
+        return hash_equals($components->hmac, $this->_generateHmac($components, $hmacKey));
+	}
 }

+ 2 - 8
lib/RNCryptor/Encryptor.php

@@ -78,8 +78,7 @@ class Encryptor extends Cryptor {
 				break;
 	
 			case 'cbc':
-				$paddedPlaintext = $this->_addPKCS7Padding($plaintext, strlen($components->headers->iv));
-				$components->ciphertext = mcrypt_encrypt($this->_settings->algorithm, $encKey, $paddedPlaintext, 'cbc', $components->headers->iv);
+                $components->ciphertext = $this->_encrypt_internal($encKey, $plaintext, 'cbc', $components->headers->iv);
 				break;
 		}
 
@@ -106,11 +105,6 @@ class Encryptor extends Cryptor {
 	}
 
 	private function _generateIv($blockSize) {
-		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
-			$randomSource = MCRYPT_RAND;
-		} else {
-			$randomSource = MCRYPT_DEV_URANDOM;
-		}
-		return mcrypt_create_iv($blockSize, $randomSource);
+        return openssl_random_pseudo_bytes($blockSize);
 	}
 }