Checking hashed algorithm problem

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

For one of my script I need to check which hash algorithms are installed on my server so I'm trying the following code which should work, but doesn't. Any help with this?
PHP Code: [ Select ]
<?php
       $hashes = hash_algos();
        $functions = get_defined_functions();
        $hashed = array();
        foreach($hashes as $id => $hash)
        {
            if(in_array($hash, $functions))
            {
                $hashed[$id] = $hash;
            }
        }
        print_r($hashed);
?>
  1. <?php
  2.        $hashes = hash_algos();
  3.         $functions = get_defined_functions();
  4.         $hashed = array();
  5.         foreach($hashes as $id => $hash)
  6.         {
  7.             if(in_array($hash, $functions))
  8.             {
  9.                 $hashed[$id] = $hash;
  10.             }
  11.         }
  12.         print_r($hashed);
  13. ?>

All it prints is an empty array with a value with no KEY and no VALUE
Above Script wrote:
Array
(
[] =>
)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Nevermind, forgot to put a simple 'internal' since $functions has an array within an array...
PHP Code: [ Select ]
<?php
      $hashes = hash_algos();
      $functions = get_defined_functions();
      $hashed = array();
      foreach($hashes as $id => $hash)
      {
         if(in_array($hash, $functions['internal']))
         {
            $hashed[$id] = $hash;
         }
      }
      print_r($hashed);
?>
  1. <?php
  2.       $hashes = hash_algos();
  3.       $functions = get_defined_functions();
  4.       $hashed = array();
  5.       foreach($hashes as $id => $hash)
  6.       {
  7.          if(in_array($hash, $functions['internal']))
  8.          {
  9.             $hashed[$id] = $hash;
  10.          }
  11.       }
  12.       print_r($hashed);
  13. ?>

That works perfectly :D
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
<?php

$built_in = new StdClass();
$data     = array();
$hash_count= 1000;
$data_len = 32;

for($i = 0; $i < $hash_count; $i++)
{
    $str = '';
    for($j = 0; $j < $data_len; $j++)
    {
        $str .= chr(rand());
    }
    $data[] = $str;
}

foreach(hash_algos() as $algo)
{
    if(function_exists($algo))
    {
        $built_in->$algo = true;
    }
}

foreach($built_in as $algo => $null)
{
    echo "$algo [hash]: ";
    $start = time() + microtime(true);
    foreach($data as $item)
    {
        $item = hash($algo, $item);
    }
    echo ((time() + microtime(true)) - $start) . " seconds\n";
    
    echo "$algo [built-in]: ";
    $start = time() + microtime(true);
    foreach($data as $item)
    {
        $item = $algo($item);
    }
    echo ((time() + microtime(true)) - $start) . " seconds\n\n";
}

?>
  1. <?php
  2. $built_in = new StdClass();
  3. $data     = array();
  4. $hash_count= 1000;
  5. $data_len = 32;
  6. for($i = 0; $i < $hash_count; $i++)
  7. {
  8.     $str = '';
  9.     for($j = 0; $j < $data_len; $j++)
  10.     {
  11.         $str .= chr(rand());
  12.     }
  13.     $data[] = $str;
  14. }
  15. foreach(hash_algos() as $algo)
  16. {
  17.     if(function_exists($algo))
  18.     {
  19.         $built_in->$algo = true;
  20.     }
  21. }
  22. foreach($built_in as $algo => $null)
  23. {
  24.     echo "$algo [hash]: ";
  25.     $start = time() + microtime(true);
  26.     foreach($data as $item)
  27.     {
  28.         $item = hash($algo, $item);
  29.     }
  30.     echo ((time() + microtime(true)) - $start) . " seconds\n";
  31.     
  32.     echo "$algo [built-in]: ";
  33.     $start = time() + microtime(true);
  34.     foreach($data as $item)
  35.     {
  36.         $item = $algo($item);
  37.     }
  38.     echo ((time() + microtime(true)) - $start) . " seconds\n\n";
  39. }
  40. ?>
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

So, it's always going to be those three no matter where you are ... thanks Joebert :)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 59 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.