Пример #1
0
 static function Hash($data, $rounds, $mintime)
 {
     $data = (string) $data;
     if ($data == "") {
         return array("success" => false, "error" => "No data.");
     }
     // Expand data.
     $origdata = $data;
     while (strlen($data) < 56) {
         $data .= $origdata;
     }
     $maxpos = strlen($data);
     $data .= $data;
     // Run through Blowfish.
     $result = "";
     for ($x = 0; $x < 32; $x++) {
         $result .= chr($x);
     }
     $x = 0;
     $ts = microtime(true) + $mintime / 1000;
     $totalrounds = 0;
     if (self::IsMcryptAvailable()) {
         $mp = mcrypt_module_open(MCRYPT_BLOWFISH, "", MCRYPT_MODE_ECB, "");
         $iv = str_repeat("", mcrypt_enc_get_iv_size($mp));
         if (mcrypt_enc_get_key_size($mp) != 56) {
             return array("success" => false, "error" => "Unexpected response from PHP function.");
         }
         while ($rounds > 0) {
             $key = substr($data, $x, 56);
             $x = ($x + 56) % $maxpos;
             mcrypt_generic_init($mp, $key, $iv);
             $result = mcrypt_generic($mp, $result);
             mcrypt_generic_deinit($mp);
             $result = substr($result, -1) . substr($result, 0, -1);
             $rounds--;
             $totalrounds++;
             if (!$rounds && $mintime > 0 && microtime(true) < $ts) {
                 $rounds++;
             }
         }
         mcrypt_module_close($mp);
     } else {
         $numbits = 56 * 8;
         $bf = new Blowfish();
         while ($rounds > 0) {
             $key = substr($data, $x, 56);
             $x = ($x + 56) % $maxpos;
             $bf->SetKey($key, $numbits);
             $bf->AddData($result);
             $bf->Finalize();
             $result = $bf->Encrypt();
             $result = substr($result, -1) . substr($result, 0, -1);
             $rounds--;
             $totalrounds++;
             if (!$rounds && $mintime > 0 && microtime(true) < $ts) {
                 $rounds++;
             }
         }
     }
     return array("success" => true, "hash" => $result, "rounds" => $totalrounds);
 }
Пример #2
0
 function benchmark($length = 100000)
 {
     //1000 Byte String
     $string = str_pad("", $length, "text");
     //Key-Setup
     $start1 = time() + (double) microtime();
     $blowfish = new Blowfish("key");
     $end1 = time() + (double) microtime();
     //Encryption
     $start2 = time() + (double) microtime();
     $blowfish->Encrypt($string);
     $end2 = time() + (double) microtime();
     echo "Keysetup: " . round($end1 - $start1, 2) . " seconds <br>";
     echo "Encrypting " . $length . " bytes: " . round($end2 - $start2, 2) . " seconds (" . round($length / ($end2 - $start2), 2) . " bytes/second)<br>";
     echo "Total: " . round($end2 - $start1, 2) . " seconds (" . round($length / ($end2 - $start1), 2) . " bytes/second)";
 }