I am encrypting some data and obtaining vastly differing results in srcipt running time between systems.
Running my algorithm on a win7 machine the encryption completes in 3-8 thousands of a second.
Same code on linux (ubuntu11 and debian6 boxes) takes between 7 and 35 SECONDS.
This is not really acceptable for my needs and was wondering if any kind person could shed any light.
Relevant code below:
<?php
class MyEncryption
{
public function __construct( $keyData )
{
$this->_encryptInit( $keyData );
}
private function _encryptInit( $keyData )
{
$this->ch = mcrypt_module_open('rijndael-256', '', MCRYPT_MODE_ECB , '');
$vector = mcrypt_create_iv (mcrypt_enc_get_iv_size( $this->ch ), MCRYPT_DEV_RANDOM );
$keySize = mcrypt_enc_get_key_size( $this->ch );
$key = substr( hash('SHA512', $keyData . $keySize ), 0, $keySize );
mcrypt_generic_init( $this->ch, $key, $vector );
}
private function _encryptClose()
{
mcrypt_generic_deinit( $this->ch );
mcrypt_module_close( $this->ch );
}
public function encryptData( $data )
{
$safeData = mcrypt_generic( $this->ch, $data );
$this->_encryptClose();
return $safeData;
}
public function decryptData( $safeData )
{
$data = mdecrypt_generic( $this->ch, $safeData );
$this->_encryptClose();
return $data;
}
}
running this code is where I see the discrepancies:
<?php
echo microtime(). ' -- Start || '.PHP_EOL;
$enc = new MyEncryption( 'astring' );
echo microtime(). ' -- Init || '.PHP_EOL;
$data = array( 'dob'=>'1970-01-01','creditcardno'=>'4000123412345678' );
$safeData = $enc->encryptData( json_encode( $data ) );
echo microtime(). ' -- Encrypted || '.PHP_EOL;
echo ' == ' . $safeData . ' == '.PHP_EOL;
$dec = new MyEncryption( 'astring' );
echo microtime(). ' -- Init2 || '.PHP_EOL;
$data = json_decode( $dec->decryptData( trim( $safeData ) ) );
echo microtime(). ' -- Decrypted || '.PHP_EOL;
echo ' == ' . $data . ' == '.PHP_EOL;
Any pointers would be most gratefully welcome..
Found the solution!!
Bit odd but I understand why this is so.
It is the method used to generate random data based on MCRYPT_DEV_RANDOM.
the answer (or at least what worked for me) lies in this article
Short story use MCRYPT_DEV_URANDOM instead.
MCRYPT_DEV_RANDOM on *nix blocks until sufficient data in the entropy pool is available. URANDOM makes it own if there isn't any.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With