php bin2hex, base64_encode; different input same output (in loop)?
I trying to create an array of filename hashes (key) and filenames (value), but most functions I use don't seem to work like I want them to...
Why are these functions when used in a loop resulting in the same output string while the input string varies? md5 and sha1 don't have this problem, but aren't reversible and that's needed.
foreach ($files as $file)
{
debug(array(bin2hex($file), $file));
}
// result
app/views/helpers/monolith.php (line 45)
Array
(
[0] => 2f686f6d652f6d746572736d697474656e2f7075626c69635f68746d6c2f6170702f707269766174652f6d622f323031302f31322e706466
[1] => /home/mtersmitten/public_html/app/private/mb/2010/12.pdf
)
app/views/helpers/monolith.php (line 45)
Array
(
[0] => 2f686f6d652f6d746572736d697474656e2f7075626c69635f68746d6c2f6170702f707269766174652f6d622f323031302f31312e706466
[1] => /home/mtersmitten/public_html/app/private/mb/2010/11.pdf
)
app/views/helpers/monolith.php (line 45)
Array
(
[0] => 2f686f6d652f6d746572736d697474656e2f7075626c69635f68746开发者_运维百科d6c2f6170702f707269766174652f6d622f323031302f31302e706466
[1] => /home/mtersmitten/public_html/app/private/mb/2010/10.pdf
)
I hope this example is more clear...
In fact the strings are different. You should check more carefully. They are the same for the most part, because both bin2hex and base64_encode encode the sequence of bytes and do not generate a hash like sha1 or md5.
bin2hex just converts every character in the string to its hex value and as for base64, check the wikipedia article to see exactly why the string is the same for large part of the outcome
... f6d622f323031302f31322e706466
^
1 2 . p h p
... f6d622f323031302f31312e706466
^
1 1 . p h p
... f6d622f323031302f31302e706466
^
1 0 . p h p
Your "hashes" are different at the spot I've indicated. bin2hex isn't encryption or hashing, it simply takes each character of the input string and converts it into the string version of its hexadecimal ascii code.
精彩评论