PHP Hash Function Help

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

PHP Hash Function Help

Post by barneystinson19 on Fri Nov 23, 2012 1:03 pm
([msg=71256]see PHP Hash Function Help[/msg])

Hello everyone!
I made a hash algorithm and made the program in vb.net and converted it in PHP.
I don't know why but it returns 0 value all the time. Here is the code.
Code: Select all
<?php
function Convert_Hash($str) {
$asc = 0;
$sum = 0;
$fillnum = 0;
$num0 = 0;
$bin = "";
$lst = "";
$haash = "";
foreach($str as &$harf) {
$sum += ord($harf);
}
$fillnum = $sum % 8;
if(strlen($str) < 16) {
$str = str_pad($str, 16, (string)$fillnum, STR_PAD_RIGHT);
}
elseif(strlen($str) > 16) {
$str = substr($str, 8);
$str = str_pad($str, 16, (string)$fillnum, STR_PAD_RIGHT);
}

foreach($str as &$letter) {
$num0 += ord($letter);
$modul += $num0 % 256;
$bin = decbin($modul);
if(strlen($bin) < 8){
$bin = str_pad($bin, 8, "0", STR_PAD_LEFT);
}
elseif(strlen($bin) > 8){
$bin = substr($bin, 8);
}
$lst +=  substr($bin, 0) ^ substr($bin, 1);
$lst +=  substr($bin, 2) ^ substr($bin, 3);
$lst +=  substr($bin, 4) ^ substr($bin, 5);
$lst +=  substr($bin, 6) ^ substr($bin, 7);
}

for($i = 0; $i < 64; $i += 4){
$asc = bindec(substr($bin, $i, 4)) + 48;
$haash += chr($asc);
}

return $haash;
}

?>


I don't know whats wrong. I'd be glad if you can help me.
Thank you :)
"Ömür dediğin üç gündür, dün geldi geçti yarın meçhuldür…O halde ömür dediğin bir gündür,o da bugündür…."
User avatar
barneystinson19
Experienced User
Experienced User
 
Posts: 50
Joined: Fri Nov 02, 2012 3:49 pm
Blog: View Blog (0)


Re: PHP Hash Function Help

Post by not_essence2 on Fri Nov 23, 2012 2:15 pm
([msg=71258]see Re: PHP Hash Function Help[/msg])

I guarantee you my reply will be full of holes, as I haven't studied up PHP in a while:
Did you declare all the variables used before the line that used them? Like str and harf?
Probably I missed something in the manual. I'm almost afraid to reply.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: PHP Hash Function Help

Post by barneystinson19 on Fri Nov 23, 2012 3:10 pm
([msg=71259]see Re: PHP Hash Function Help[/msg])

I didn't declare which variables I used in foreach loop like $harf. I needn't to declare a variable if I'm going to use it in for each loop in vb.net so I thought I needn't to here too. Don't afraid about giving answer I copied the original code We're free to try :mrgreen:

I declared $harf and $letter before I use them in foreach loop but it still returns 0 :(

-- Fri Nov 23, 2012 4:44 pm --

I found the problem and fix it. Problem solved :D
"Ömür dediğin üç gündür, dün geldi geçti yarın meçhuldür…O halde ömür dediğin bir gündür,o da bugündür…."
User avatar
barneystinson19
Experienced User
Experienced User
 
Posts: 50
Joined: Fri Nov 02, 2012 3:49 pm
Blog: View Blog (0)


Re: PHP Hash Function Help

Post by not_essence2 on Fri Nov 23, 2012 4:56 pm
([msg=71262]see Re: PHP Hash Function Help[/msg])

Oh? What was the problem?
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: PHP Hash Function Help

Post by barneystinson19 on Fri Nov 23, 2012 5:10 pm
([msg=71263]see Re: PHP Hash Function Help[/msg])

The problem was this:

Code: Select all
$lst +=  substr($bin, 0) ^ substr($bin, 1);
$lst +=  substr($bin, 2) ^ substr($bin, 3);
$lst +=  substr($bin, 4) ^ substr($bin, 5);
$lst +=  substr($bin, 6) ^ substr($bin, 7);


I fixed it like this

Code: Select all
$lst = $lst . strval(substr($bin, 0) ^ substr($bin, 1));
$lst = $lst . strval(substr($bin, 2) ^ substr($bin, 3));
$lst = $lst . strval(substr($bin, 4) ^ substr($bin, 5));
$lst = $lst . strval(substr($bin, 6) ^ substr($bin, 7));


So the $lst value is a binary now and it converts it to decimal then to char. But at the first code, It was adding val to $lst like it is an integer. So it returned 0 value :D

Btw I changed the algorithm a little.

Here is the link If you want to try:
http://w5pages.com/conversion.php
"Ömür dediğin üç gündür, dün geldi geçti yarın meçhuldür…O halde ömür dediğin bir gündür,o da bugündür…."
User avatar
barneystinson19
Experienced User
Experienced User
 
Posts: 50
Joined: Fri Nov 02, 2012 3:49 pm
Blog: View Blog (0)


Re: PHP Hash Function Help

Post by not_essence2 on Fri Nov 23, 2012 5:15 pm
([msg=71264]see Re: PHP Hash Function Help[/msg])

Ohhhhhh. Base adding error.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: PHP Hash Function Help

Post by barneystinson19 on Fri Nov 23, 2012 5:31 pm
([msg=71265]see Re: PHP Hash Function Help[/msg])

Yeah :D I just started learning Php :)
"Ömür dediğin üç gündür, dün geldi geçti yarın meçhuldür…O halde ömür dediğin bir gündür,o da bugündür…."
User avatar
barneystinson19
Experienced User
Experienced User
 
Posts: 50
Joined: Fri Nov 02, 2012 3:49 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron