note 52680 added to function.sha1-file

From: Date: Mon, 09 May 2005 18:59:55 +0000
Subject: note 52680 added to function.sha1-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-89172@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.sha1-file.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52680 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52680&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52680&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52680&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52680&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52680&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52680&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89172) next »