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.
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 can't really do much to edit your function because it has so many elements I haven't seen before.
/** * 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)); }
123/10 = 12 R 3 12/10 = 1 R 2 1 --------------- 1 R2 R3 or 123
123/10 = 12 R 3
$number = floor(123/10); // 12 $remainder = 123%10; // 3
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); }
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); }
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); }
$v=($charconvert[$char[0]]*$charconvert[$char[1]])+32;
compress_string('42'); // p compress_string('24'); // p
$v=($charconvert[$char[0]]*$charconvert[$char[1]])+32-$tmp;
$_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"; }
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); }
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); }
function compress($sha) { $parts = str_split($sha, 20); return $parts[0]; }
| DaniWeb Message | |
| Cancel Changes | |