密码类和mcrypt_create_iv有时很慢 [英] cipher class and mcrypt_create_iv is slow at times

查看:202
本文介绍了密码类和mcrypt_create_iv有时很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的密码类有一个问题。有时它非常快。然而有时候很慢。我使用的代码如下

  class Cipher {
private $ securekey,$ iv;
函数__construct(){
$ this-> securekey = hash('sha256','51(^ 8k12cJ [6& cvo3H /!2s02Uh46vuT4l7sc7a @ cZ27Q',TRUE);
$ this-> iv = mcrypt_create_iv(32);
}
函数加密($ input){
返回base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$ this-> securekey,$ input,
}
函数解密($ input){
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$ this-> securekey,base64_decode($ input),MCRYPT_MODE_ECB));
}
函数storeIV(){
return $ this-> iv;
}
}

有什么建议,为什么这可能是缓慢的,我可以如何解决这个问题?

解决方案

您是否尝试过 <$ c $的三个不同的第二个参数c> mcrypt_create_iv() MCRY PT_RAND (系统随机数生成器), MCRYPT_DEV_RANDOM (从/ dev / random读取数据)和 MCRYPT_DEV_URANDOM (从/ dev / urandom读取数据)?他们提供不一样的速度吗?我不知道是不是因为/ dev / random(默认的随机源)已经用完了收集的熵;该功能将会阻止。


I am having an issue with my cipher class. At times it is very fast. Sometimes however it is slow. the code Im using is as follows

class Cipher {
    private $securekey, $iv;
    function __construct() {
        $this->securekey = hash('sha256','51(^8k"12cJ[6&cvo3H/!2s02Uh46vuT4l7sc7a@cZ27Q',TRUE);
        $this->iv = mcrypt_create_iv(32);
    }
    function encrypt($input) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB));
    }
    function decrypt($input) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB));
    }
    function storeIV() {
        return $this->iv;
    }
}

Are there any suggestions on why this may be slow at times and how I could fix this?

解决方案

Have you tried the three different second arguments for mcrypt_create_iv(): MCRYPT_RAND (system random number generator), MCRYPT_DEV_RANDOM (read data from /dev/random) and MCRYPT_DEV_URANDOM (read data from /dev/urandom)? Do they offer different consistent speeds? I wonder if it's because /dev/random (the default random source) is running out of collected entropy; the function will block when it does.

这篇关于密码类和mcrypt_create_iv有时很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆