note 52680 deleted from function.sha1-file by derek
| From: | derek@php.net | Date: | Tue, 10 May 2005 00:15:11 +0000 |
| Subject: | note 52680 deleted from function.sha1-file by derek | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-89184@lists.php.net to get a copy of this message | ||
Note Submitter: jakob_konkur at hotmail dot com
Reason: bad code
----
I did some checking on the performance of the sha1_file-function versus this one:
<?php
/****
This is my own defined hash-function.
It's about 20 times faster then sha1_file, and it returns an integer, which
is easy (read fast versus a varchar-index) to use as an index in fx. a database
Returns -1 if anything goes wrong
*****/
function ownHashFunction($strFileName) {
//Amount of bytes gather from the file
$intAmountOfBytes = 4096;
//The max length of the hashcode, has to use bcpw, to get the right number
$intSizeOfHashCode = bcpow(10,20);
$filename = $strFileName;
if(!$handle = fopen($filename, "rb"))
return -1;
$intFileSize = filesize($filename);
/*If the size is smaller then 4 times 4096 (which is the amount of data we're using in the
xor'ring)
we have to adjust it according to the size of the file.*/
if ($intFileSize < 4* $intAmountOfBytes) {
$intContentLength = $intFileSize / 4;
$content1 = fread($handle, $intContentLength);
$content2 = fread($handle, $intContentLength);
$content3 = fread($handle, $intContentLength);
$content4 = fread($handle, $intContentLength);
} else {
//Reads 4 * $intAmountOfBytes (4096) from the beginning of the file
$intContentLength = $intAmountOfBytes;
$content1 = fread($handle, $intContentLength);
$content2 = fread($handle, $intContentLength);
$content3 = fread($handle, $intContentLength);
$content4 = fread($handle, $intContentLength);
}
if(!fclose($handle)) return -1;
//Bitwise Xor the shit, convert it to dec and mod $intSizeOfHashCode), so it can be used as a index
in the db.
return bcmod(bindec((($content1 ^ $content2) ^ ($content3 ^ $content4))), $intSizeOfHashCode);
}
?>
It takes out 4 parts of the file and xor them in pairs. Sure it doesn't do a hashCode for the
hole file, but as in my case, it's just enough for a hashCode to use in a database to unique
identify a picture. And as stated it is about 20 times faster then sha1_file and about 16 times
faster the md5_file. ownHashFunction can undoubtful be improved because of the use of the
bc-package, but I couldn't see how to get around the limit of the integer-type (only 2^32 big,
which isn't enough) - maybe someone else can?
Cheers