2014-10-07 65 views
0

我有一个类有以下__construct。mcrypt长时间初始化模块

final class mydecoder { 
    private $td; 
    public function __construct($key){ 
     /* Open the cipher */ 
     $this->td = mcrypt_module_open(MCRYPT_BLOWFISH, '', 'ecb', ''); 
     /* Create the IV and determine the keysize length, use MCRYPT_RAND 
     * on Windows instead */ 
     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_DEV_RANDOM); 
     $ks = mcrypt_enc_get_key_size($this->td); 
     /* Create key */ 
     $key = substr(md5($key), 0, $ks); 
     /* Intialize encryption */ 
     mcrypt_generic_init($this->td, $key, $iv); 
    } 
} 

当我这样称呼它:

$encoder = new myfish('mykey1'); 
$encoder = new myfish('mykey2'); 

我有以下问题。

开放页首次

construct #1 execution time: 5s 
construct #2 execution time: 0s 

按F5

construct #1 execution time: 0s 
construct #2 execution time: 14s 

按F5

construct #1 execution time: 5s 
construct #2 execution time: 14s 

糖化F5 3次(排队)

construct #1 execution time: 15s 
construct #2 execution time: 45s 

它看起来像队列?它是如何工作的?也许我没有正确使用它?我没有与密码太多的经验,因为我从来没有:)

回答

1

的问题是由初始化向量(IV)genertor造成需要它,也就是这条线:

$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_DEV_RANDOM); 

如果使用MCRYPT_DEV_RANDOM模式,PHP会一直等到有足够的熵安全。

但是,如果将模式更改为MCRYPT_DEV_URANDOM(安全性较低),但是如果熵变得太低,则不会等待,从而提高速度。

+0

所以基本上,使用'MCRYPT_DEV_RANDOM'会使延迟更远,所以随机距离彼此“更远”? – Grzegorz 2014-10-07 10:36:47

+1

下面是我为您找到的更好的解释:http://stackoverflow.com/a/11173861/2332336 – Latheesan 2014-10-07 10:40:09