sha1_file

(PHP 4 >= 4.3.0, PHP 5)

sha1_fileВозвращает SHA1-хэш файла

Описание

string sha1_file ( string $filename [, bool $raw_output = false ] )

Вычисляет SHA1-хэш файла, имя которого задано аргументом filename, используя алгоритм » US Secure Hash Algorithm 1 и возвращает этот хэш.

Список параметров

filename

Имя хэшируемого файла.

raw_output

Если установлен в TRUE, возвращает 20-символьный хэш в бинарном формате.

Возвращаемые значения

Возвращает строку в случае успеха, иначе возвращается FALSE.

Примеры

Пример #1 Пример использования sha1_file()

<?php
foreach(glob('/home/Kalle/myproject/*.php') as $ent)
{
    if(
is_dir($ent))
    {
        continue;
    }

    echo 
$ent ' (SHA1: ' sha1_file($ent) . ')'PHP_EOL;
}
?>

Список изменений

Версия Описание
5.1.0 Функция теперь работает с потоковым API. Это означает, что ее можно использовать с обертками, например, sha1_file('http://example.com/..')
5.0.0 Добавлен параметр raw_output

Смотрите также

  • sha1() - Возвращает SHA1-хэш строки
  • md5_file() - Возвращает MD5-хэш файла
  • crc32() - Вычисляет полином CRC32 для строки

Коментарии

P2P programs like LimeWire utilize sha1 to identify files. However they do it in base32. Here's an utility class if you want to write a Gnutella client in PHP5

/**
Utility base32 SHA1 class for PHP5
Copyright (C) 2006  Karl Magdsick (original author for Python)
                    Angel Leon (ported to PHP5)
                    Lime Wire LLC

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
class SHA1 {
  static $BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

  /** Given a file it creates a magnetmix */
  static function fileSHA1($file) {
    $raw = sha1_file($file,true);
    return SHA1::base32encode($raw);
  } //fileSHA1

  /** Takes raw input and converts it to base32 */
  static function base32encode($input) {
    $output = '';
    $position = 0;
    $storedData = 0;
    $storedBitCount = 0;
    $index = 0;

    while ($index < strlen($input)) {
      $storedData <<= 8;
      $storedData += ord($input[$index]);
      $storedBitCount += 8;
      $index += 1;

      //take as much data as possible out of storedData
      while ($storedBitCount >= 5) {
        $storedBitCount -= 5;
        $output .= SHA1::$BASE32_ALPHABET[$storedData >> $storedBitCount];
        $storedData &= ((1 << $storedBitCount) - 1);
      }
    } //while

    //deal with leftover data
    if ($storedBitCount > 0) {
      $storedData <<= (5-$storedBitCount);
      $output .= SHA1::$BASE32_ALPHABET[$storedData];
    }

    return $output;
  } //base32encode

}
2006-02-10 09:02:53
http://php5.kiev.ua/manual/ru/function.sha1-file.html
checking the sha1 of the file. ideal for download scripts making sure the file the user is downloading is not currupt
by checking the sha1 key

<?php
$filename 
'./Path/To/Your/File.zip';

if (
file_exists($filename)) {
echo
"i see the file";
} else {
echo 
"recheck the link file maybe broken";
}
$file sha1_file('./Path/To/Your/File.zip');

echo
"<br>is file hash valid?";
echo
"hash = add the hash here<br>";
echo
"hash of file :";
print(
$file);
?>
2006-08-04 23:02:25
http://php5.kiev.ua/manual/ru/function.sha1-file.html
If you've got a script that allows user file upload, and you want to prevent multiple uploads of the same file:

    <?
        session_start
();
       
$isDuplicate false;
        if(isset(
$_FILES["filename"]["tmp_name"]) && file_exists($_FILES["filename"]["tmp_name"])) {
           
$fileHash sha1_file($_FILES["filename"]["tmp_name"]);
            if(!isset(
$_SESSION["check_filelist"])) {
               
$_SESSION["check_filelist"] = array($fileHash);
            }
            elseif(
in_array($fileHash,$_SESSION["check_filelist"])) {
               
$isDuplicate true;   
            }
            else {
               
$_SESSION["check_filelist"][] = $fileHash;   
            }
           
            if(
$isDuplicate) {
                echo 
"You've already uploaded that file";   
            }
            else{
               
// do some stuff   
           
}
        }
       
   
?>
2007-09-06 10:02:44
http://php5.kiev.ua/manual/ru/function.sha1-file.html
<?php
     
if(!function_exists('sha1_file'))
     {
          function 
sha1_file($file null)
          {
               if(
is_null($file) || !file_exists($file))
               {
                    return 
trigger_error('File is null or does not exists');
               }

               return 
sha1(file_get_contents($file));
          }
     }
?>

Remember, it's important to write a corret url like this:
/home/snackzon/public_html/php/sha1.php
2008-11-23 06:05:21
http://php5.kiev.ua/manual/ru/function.sha1-file.html
Just for the record - 

As some have pointed out, you have two ways to generate the hash of a file:
Method 1 [this function]: sha1_file($file)
Method 2: sha1(file_get_contents($file))

It's important to realize that these two methods are NOT the same thing. If they were, I seriously doubt this function would exist.

The key difference, as far as I can tell, is how the file's contents are loaded. The second method loads the entirety of $file into memory before passing it to sha1($str). Method two, however, loads the contents of $file as they are needed to create the hash.

If you can guarantee that you'll only ever have to hash relatively small files, this difference means very little. If you have larger ones, though, loading the entirety of file into memory is a bad idea: best case, you slow down your server as it tries to handle the request; worse case, you run out of memory and don't get your hash at all.

Just try to keep this in mind if you decide to load the file's contents yourself, in lieu of using this function. On my system, I was able to use this function to generate the hash of a 2.6GB file in 22 seconds, whereas I could not with the second method, due to an out-of-memory error (which took 185 seconds).
2011-07-05 10:13:33
http://php5.kiev.ua/manual/ru/function.sha1-file.html

    Поддержать сайт на родительском проекте КГБ