| | |
Dehasher script malfunctioning
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
•
•
I just discovered that when using ascii_base() on the crc32 hash of 9 and 0 they both end up with blank strings which is a bit of a bug. For now I might try and refine my compression function. I can't really do much to edit your function because it has so many elements I haven't seen before.
However, I've noticed that the function does not work for very large integers due to PHP not being able to do arithmetic on them.
There are some work arounds to this in the comments on:
http://www.php.net/manual/en/function.base-convert.php
If you have bcmath enabled, you can rely on it to do the arithmetic correctly.
Below is the function modified to use BCMath.
PHP Syntax (Toggle Plain Text)
if (!function_exists('bcdiv')) { //echo "No BC Math\n"; function bcdiv($dividend, $divisor) { $quotient = floor($dividend/$divisor); return $quotient; } function bcmod($dividend, $modulo) { $remainder = $dividend%$modulo; return $remainder; } } else { //echo "Using BC Math\n"; } /** * Convert Decimal to a base less then 255 comprised of ASCII chars * * @param Int $num * @param Int $base (2-255) * @return ASCII String */ function base255($num, $base = 255) { if ($num < 0) $num = -$num; $ret = array(); while($num > $base) { $rem = bcmod($num, $base); $num = bcdiv($num, $base); $ret[] = chr($rem); } $ret[] = chr($num); return implode('', array_reverse($ret)); }
I renamed it to base255 so it makes more sense. It should now give you correct values if you have bcmath.
I just profiled the function. It doesn't seem to use much memory at all. Just around 260Kb at the most. I tested both with and without BCMath. Are you sure it isn't something else?
Last edited by digital-ether; Oct 7th, 2009 at 12:19 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
•
•
I can't really do much to edit your function because it has so many elements I haven't seen before.
The only odd operations used are:
% - modulo or remainder
chr() - return the character represented by a number in ASCII table
floor() - round down the float to an int
The modulo returns the remainder after dividing
eg: 5%2 = 1
ie: 5/2 = 2 remainder 1
chr(96) = a
The letter a is represented by the number 96 in ASCII
Something like:
$chr = array(96=>'a', 97=>'b' ... 255);
so chr(96) = $chr[96];
And floor just removes everything after the decimal point.
eg: 5/2 = 2.5
floor(2.5) = 2
Here is the function with comments:
PHP Syntax (Toggle Plain Text)
/** * Convert Decimal to a base less then 255 comprised of ASCII chars * * @param Int $num * @param Int $base (2-255) * @return ASCII String */ function base255($num, $base = 255) { // remove the negative sign by multiplying by -1 if $num is negative if ($num < 0) $num = -$num; // an array to hold the digits of the new number $ret = array(); // while the number is larger then our base, we just keep dividing it by the base while($num > $base) { // get the remainder after dividing by the base $rem = bcmod($num, $base); // divide by the base to move up one unit $num = bcdiv($num, $base); // the remainders of each division, make up the new number // we save the character the remainder represents in ASCII so we only have to save one character, instead of the number $ret[] = chr($rem); } // since the number is less then the base, it is the remainder itself $ret[] = chr($num); // we reverse the order of chars, since we started calculating remainders from the smallest unit return implode('', array_reverse($ret)); }
I think its simplest to look at it when converting base 10 to base 10.
123 would be:
123/10 = 12 R 3 12/10 = 1 R 2 1 --------------- 1 R2 R3 or 123
In order to do the first line with PHP:
123/10 = 12 R 3 We need to do:
PHP Syntax (Toggle Plain Text)
$number = floor(123/10); // 12 $remainder = 123%10; // 3
I hope that helps.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
I managed to make a better function which doesn't have the gap symbols and is as follows:
The above function I made compresses it to half the size and skips the first 32 characters on the ascii table which are useless to me. I will try this function for a few days and see how it works and hopefully this will be the function.
php Syntax (Toggle Plain Text)
function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7,'2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr)) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); unset($arr[$i]); $v=($charconvert[$char[0]]*$charconvert[$char[1]])+32; if ($v<256) { $str[]=chr($v); } else { $str[]=chr($charconvert[$char[0]]); $arr[$i]=$char[1]; $arr=implode('',$arr); } } } return implode('',$str); }
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
Previous post Edit:
I discovered my function had a few memory leeks and fixed it to end up being the following:
I discovered my function had a few memory leeks and fixed it to end up being the following:
php Syntax (Toggle Plain Text)
function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7, '2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr[0]) || $arr[0]===0) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); $arr[$i]=''; $v=($charconvert[$char[0]]*$charconvert[$char[1]])+32; if ($v<256) { $str[]=chr($v); } else { $str[]=$char[0]; $arr[$i]=$char[1]; $arrs=implode('',$arr); unset($arr); $arr=str_split($arrs,2); unset($arrs); } unset($v); } } unset($arr,$char,$charconvert); return implode('',$str); }
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
•
•
•
•
Previous post Edit:
I discovered my function had a few memory leeks and fixed it to end up being the following:
php Syntax (Toggle Plain Text)
function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7, '2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr[0]) || $arr[0]===0) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); $arr[$i]=''; $v=($charconvert[$char[0]]*$charconvert[$char[1]])+32; if ($v<256) { $str[]=chr($v); } else { $str[]=$char[0]; $arr[$i]=$char[1]; $arrs=implode('',$arr); unset($arr); $arr=str_split($arrs,2); unset($arrs); } unset($v); } } unset($arr,$char,$charconvert); return implode('',$str); }
PHP Syntax (Toggle Plain Text)
$v=($charconvert[$char[0]]*$charconvert[$char[1]])+32;
eg:
PHP Syntax (Toggle Plain Text)
compress_string('42'); // p compress_string('24'); // p
I'm not sure what you're after, so the alternatives I've given are generalizations.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Thanks for pointing that out but I should be able to code a reader that can filter the incorrect matches. So as you pointed out the string "aecd" would also have the same result as "eadc" but wouldn't "aecd" wouldn't match "aced". Also the normal work around if there were enough symbols is the following line.
However I have another work around which is when pulling the the data from the database, to rehash the original data and to see if it matches what was requested. An example is as follows:
Also I did a test and for some reason my script does not always suffer from that bug or at least on my test. But I did alter the function a to the following:
So I guess I'm just lucky that bug doesn't happen on my all the time but still will add that second validator. I also calculated that in 13 days I can fill 30 GB of dehashing data calculating to at least 5 digits. So the script works for my needs even with it's bug of reverse characters having same match. Because mysql can still filter the results from millions of rows to a few dozen it should still do the job.
PHP Syntax (Toggle Plain Text)
$v=($charconvert[$char[0]]*$charconvert[$char[1]])+32-$tmp;
php Syntax (Toggle Plain Text)
$_GET['q']=trim($_GET['q']); function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7,'2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr[0]) || $arr[0]===0) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); $arr[$i]=''; if (empty($charconvert[$char[1]])) { $tmp=1; } else { $tmp=$charconvert[$char[1]]; } $v=($charconvert[$char[0]]*$tmp)+32; if ($v<256) { $str[]=chr($v); } else { $str[]=$char[0]; $arr[$i]=$char[1]; $arrs=implode('',$arr); unset($arr); $arr=str_split($arrs,2); unset($arrs); } unset($v,$tmp); } } unset($arr,$char,$charconvert); return implode('',$str); } if ($_GET['hash']>0) { $r=mysql_query('SELECT `id` FROM `hash` WHERE `'.$hash.'`="'.mysql_real_escape_string(compress_string($_GET['q'])).'"'); } else { $r=mysql_query('SELECT `id` FROM `hash` WHERE `sha1`="'.mysql_real_escape_string(compress_string(substr($_GET['q'],0,4).hash('crc32',$_GET['q']).hash('crc32b',$_GET['q']))).'"'); } if (mysql_num_rows($r)==0) { echo '<table border=0 cellpadding=3 cellspacing=0 bgcolor="#D0D0D0"><tr bgcolor="#D0D0D0"><td bgcolor="#D0D0D0"><b>No Results found for '.htmlentities($_GET['q'],ENT_QUOTES).'</b></td></tr></table>'."\r\n"; } else { echo '<table border=1 cellpadding=2 cellspacing=0 style="border-top:1px; border-top-color:#FFFFFF"><tr bgcolor="#D0D0D0" style="font-family:arial; font-weight:bolder; border-top:1px; border-top-color:#FFFFFF"><td bgcolor="#D0D0D0">Tanslation</td><td bgcolor="#D0D0D0">SHA1</td><td bgcolor="#D0D0D0">Crc32</td><td bgcolor="#D0D0D0">Crc32b</td></tr>'."\r\n"; while ($data=mysql_fetch_assoc($r)) { if ($_GET['q']==hash($hash,$data['id'])) { echo '<tr><td bgcolor="#D0FFFF"><textarea style="width:'.((strlen($data['id'])*10)).'px; height:16px; overflow-y:hidden;" scrolling=no>'.$data['id'].'</textarea></td><td>'.hash('sha1',$data['id']).'</td><td>'.hash('crc32',$data['id']).'</td><td>'.hash('crc32b',$data['id'])."</td></tr>\r\n"; } } echo "</table>\r\n"; }
php Syntax (Toggle Plain Text)
function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7,'2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr[0]) || $arr[0]===0) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); $arr[$i]=''; if (empty($charconvert[$char[1]])) { $tmp=1; } else { $tmp=$charconvert[$char[1]]; } $v=($charconvert[$char[0]]*$tmp)+32; if ($v<256) { $str[]=chr($v); } else { $str[]=$char[0]; $arr[$i]=$char[1]; $arrs=implode('',$arr); unset($arr); $arr=str_split($arrs,2); unset($arrs); } unset($v,$tmp); } } unset($arr,$char,$charconvert); return implode('',$str); }
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
•
•
•
•
Thanks for pointing that out but I should be able to code a reader that can filter the incorrect matches. So as you pointed out the string "aecd" would also have the same result as "eadc" but wouldn't "aecd" wouldn't match "aced". Also the normal work around if there were enough symbols is the following line.
However I have another work around which is when pulling the the data from the database, to rehash the original data and to see if it matches what was requested. An example is as follows:PHP Syntax (Toggle Plain Text)
$v=($charconvert[$char[0]]*$charconvert[$char[1]])+32-$tmp;
Also I did a test and for some reason my script does not always suffer from that bug or at least on my test. But I did alter the function a to the following:php Syntax (Toggle Plain Text)
$_GET['q']=trim($_GET['q']); function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7,'2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr[0]) || $arr[0]===0) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); $arr[$i]=''; if (empty($charconvert[$char[1]])) { $tmp=1; } else { $tmp=$charconvert[$char[1]]; } $v=($charconvert[$char[0]]*$tmp)+32; if ($v<256) { $str[]=chr($v); } else { $str[]=$char[0]; $arr[$i]=$char[1]; $arrs=implode('',$arr); unset($arr); $arr=str_split($arrs,2); unset($arrs); } unset($v,$tmp); } } unset($arr,$char,$charconvert); return implode('',$str); } if ($_GET['hash']>0) { $r=mysql_query('SELECT `id` FROM `hash` WHERE `'.$hash.'`="'.mysql_real_escape_string(compress_string($_GET['q'])).'"'); } else { $r=mysql_query('SELECT `id` FROM `hash` WHERE `sha1`="'.mysql_real_escape_string(compress_string(substr($_GET['q'],0,4).hash('crc32',$_GET['q']).hash('crc32b',$_GET['q']))).'"'); } if (mysql_num_rows($r)==0) { echo '<table border=0 cellpadding=3 cellspacing=0 bgcolor="#D0D0D0"><tr bgcolor="#D0D0D0"><td bgcolor="#D0D0D0"><b>No Results found for '.htmlentities($_GET['q'],ENT_QUOTES).'</b></td></tr></table>'."\r\n"; } else { echo '<table border=1 cellpadding=2 cellspacing=0 style="border-top:1px; border-top-color:#FFFFFF"><tr bgcolor="#D0D0D0" style="font-family:arial; font-weight:bolder; border-top:1px; border-top-color:#FFFFFF"><td bgcolor="#D0D0D0">Tanslation</td><td bgcolor="#D0D0D0">SHA1</td><td bgcolor="#D0D0D0">Crc32</td><td bgcolor="#D0D0D0">Crc32b</td></tr>'."\r\n"; while ($data=mysql_fetch_assoc($r)) { if ($_GET['q']==hash($hash,$data['id'])) { echo '<tr><td bgcolor="#D0FFFF"><textarea style="width:'.((strlen($data['id'])*10)).'px; height:16px; overflow-y:hidden;" scrolling=no>'.$data['id'].'</textarea></td><td>'.hash('sha1',$data['id']).'</td><td>'.hash('crc32',$data['id']).'</td><td>'.hash('crc32b',$data['id'])."</td></tr>\r\n"; } } echo "</table>\r\n"; }
So I guess I'm just lucky that bug doesn't happen on my all the time but still will add that second validator. I also calculated that in 13 days I can fill 30 GB of dehashing data calculating to at least 5 digits. So the script works for my needs even with it's bug of reverse characters having same match. Because mysql can still filter the results from millions of rows to a few dozen it should still do the job.php Syntax (Toggle Plain Text)
function compress_string($string) { $str=array(); $charconvert=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'1'=>7,'2'=>8,'3'=>9,'4'=>10,'5'=>11,'6'=>12,'7'=>13,'8'=>14,'9'=>15,'0'=>16); $arr=str_split($string,2); while (!empty($arr[0]) || $arr[0]===0) { for ($i=0;isset($arr[$i]);$i++) { $char=str_split($arr[$i],1); $arr[$i]=''; if (empty($charconvert[$char[1]])) { $tmp=1; } else { $tmp=$charconvert[$char[1]]; } $v=($charconvert[$char[0]]*$tmp)+32; if ($v<256) { $str[]=chr($v); } else { $str[]=$char[0]; $arr[$i]=$char[1]; $arrs=implode('',$arr); unset($arr); $arr=str_split($arrs,2); unset($arrs); } unset($v,$tmp); } } unset($arr,$char,$charconvert); return implode('',$str); }
It is the same as just cutting the SHA1 in half, and keeping the first half. You will will have a low probability of collisions.
So a function like:
PHP Syntax (Toggle Plain Text)
function compress($sha) { $parts = str_split($sha, 20); return $parts[0]; }
would achieve the same.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- The name for the Resize Button to ad to Script (HTML and CSS)
- Random Programming Script (Computer Science)
- Importing SQL Script File - Urgent !! (Database Design)
- Script problems with IE (Web Browsers)
- Help with shell script to auto gzip a .sql dump after backup (Shell Scripting)
- Adding mutiple PC's to an OU via script? (Windows NT / 2000 / XP)
- Table of Contents Script (Java)
- ASP slow-down server script (ASP)
Other Threads in the PHP Forum
- Previous Thread: PHP inside HTML files
- Next Thread: chained select boxes
Views: 1842 | Replies: 26
| Thread Tools | Search this Thread |
Tag cloud for broken, dehasher, malfunction, malfunctioning, php, script
.net action advanced ajax archive array asp basic beginner broken c# class cms code cookies curl data database date display download dynamic email enter errorlog file files flash flex folder form forms function functions gentoo html image include integration java javascript jobs jquery key keywords lamp ldap limit link links linux login longisland mail matching memmory menu methods multiple mysql network news nodes oop open overwrite paypal php post provider query script search security seo session sharepoint sms soap software spam specific sql static table tutorial up-to-date upload validation variable vbulletin video virus web webdesign websitecontactform xml yahoo youtube zend







