Created
February 23, 2017 21:53
-
-
Save spidgorny/2c8968eba9bfcd6a6525341c12069ece to your computer and use it in GitHub Desktop.
RandomLib fix for PHP 7.1 which does not have mcrypt anymore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use RandomLib\Factory; | |
class MyRandomFactory extends Factory { | |
public function __construct() { | |
parent::__construct(); | |
$this->mixers = []; | |
$this->registerMixer('OpenSSLMixer', OpenSSLMixer::class); | |
// debug($this->getMixers()); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use RandomLib\AbstractMixer; | |
use SecurityLib\Strength; | |
class OpenSSLMixer extends AbstractMixer implements RandomLib\Mixer { | |
/** | |
* Get the block size (the size of the individual blocks used for the mixing) | |
* | |
* @return int The block size | |
*/ | |
protected function getPartSize() { | |
return 128/8; | |
} | |
/** | |
* Mix 2 parts together using one method | |
* | |
* @param string $part1 The first part to mix | |
* @param string $part2 The second part to mix | |
* | |
* @return string The mixed data | |
*/ | |
protected function mixParts1($part1, $part2) { | |
return $this->encryptBlock($part1, $part2); | |
} | |
/** | |
* Mix 2 parts together using another different method | |
* | |
* @param string $part1 The first part to mix | |
* @param string $part2 The second part to mix | |
* | |
* @return string The mixed data | |
*/ | |
protected function mixParts2($part1, $part2) { | |
return $this->encryptBlock($part2, $part1); | |
} | |
/** | |
* Return an instance of Strength indicating the strength of the mixer | |
* | |
* @return \SecurityLib\Strength An instance of one of the strength classes | |
*/ | |
public static function getStrength() { | |
return new Strength(Strength::MEDIUM); | |
} | |
/** | |
* Test to see if the mixer is available | |
* | |
* @return bool If the mixer is available on the system | |
*/ | |
public static function test() { | |
// debug('openssl_encrypt', function_exists('openssl_encrypt')); | |
return function_exists('openssl_encrypt'); | |
} | |
private function encryptBlock($part2, $part1) { | |
$iv = openssl_random_pseudo_bytes(16); | |
return openssl_encrypt($part2, "aes-256-cbc", $part1, OPENSSL_RAW_DATA, $iv); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment