mcrypt性能 [英] mcrypt performance

查看:56
本文介绍了mcrypt性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加密一些数据,并且在系统之间的严格运行时间上获得了截然不同的结果.

I am encrypting some data and obtaining vastly differing results in srcipt running time between systems.

在win7机器上运行我的算法,加密过程将在3-8千秒内完成.

Running my algorithm on a win7 machine the encryption completes in 3-8 thousands of a second.

Linux(ubuntu11和debian6盒)上的相同代码需要7到35秒.

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.

以下相关代码:

<?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;

非常感谢任何指针..

推荐答案

找到了解决方案!

有点奇怪,但我理解为什么会这样.

Bit odd but I understand why this is so.

这是用于基于MCRYPT_DEV_RANDOM生成随机数据的方法.

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

短篇小说,请改用MCRYPT_DEV_URANDOM.

Short story use MCRYPT_DEV_URANDOM instead.

MCRYPT_DEV_RANDOM,直到熵池中有足够的数据可用为止.如果没有,则URANDOM拥有它.

MCRYPT_DEV_RANDOM on *nix blocks until sufficient data in the entropy pool is available. URANDOM makes it own if there isn't any.

这篇关于mcrypt性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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