Wow! That REALLY did make a great difference!
Encoding a file of 2MB took 50 seconds before, now it is done in 1 (ONE)
second!
My Key is 8192 bytes long, is that the reason for why it so much faster
for me?
I don't really get how it works, but I guess I will figure it out.
Thank you very much!
-- Marcus
-Original Message-
From: Marcel Tschopp [mailto:[EMAIL PROTECTED]
Sent: den 24 juni 2004 13:37
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Optimize simple file XOR code
Hi Marcus
Try this:
function CryptFile($hash, $filename)
{
// The key used is generated using another function.
// Since this function is only called once it has
// nothing to do with the performance of this function
$key = GenerateKey($hash);
$keylen = strlen($key = implode('', $key));
$handle = fopen($filename, 'r');
while (!feof($handle))
{
// Set the time limit or the script will time out
// on large files
set_time_limit(30);
$data = fread($handle, 8192);
for ($i = 0; $i strlen($data); $i = $i + $keylen)
{
$data = substr_replace($data, substr($data, $i, $keylen) ^ $key,
$i, $keylen);
}
// Return the encrypted version to the browser
echo $data;
}
}
It should return the same results, but at my machine it is about 30
percent faster.
Cheers
Marcel
Hi!
I'm using a very simple algorithm to encrypt files sent from the
server On Demand. The algorithm uses XOR for the encryption. My
problem is that the code is very slow, it can only process about 40 KB
per second on my system, so my question is, is there any more speed
optimized way to write this code?
Here is my function:
function CryptFile($hash, $filename)
{
// The current position in the key generated below
$key_pos = 0;
// The key used is generated using another function.
// Since this function is only called once it has
// nothing to do with the performance of this function
$key = GenerateKey($hash);
$handle=fopen($filename, r);
while (!feof($handle))
{
// Set the time limit or the script will time out
// on large files
set_time_limit(30);
$data = fread($handle,8192);
for ($i=0; $istrlen($data); $i++)
{
// Change the data read from the file by using
// XOR with the key
$data[$i] = $data[$i] ^ $key[$key_pos];
// Calculate the next position in the key
$key_pos=($key_pos+1) % count($key);
}
// Return the encrypted version to the browser
echo $data;
}
}
My guess is that if one should start optimizing the code anywhere, it
would be within the for-loop. Any ideas?
Thanks in advance!
Regards,
Marcus
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php