Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: sha1_file - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net

search for in the

sha1" width="11" height="7"/> <setlocale
Last updated: Wed, 01 Nov 2006
view this page in

sha1_file

(PHP 4 >= 4.3.0, PHP 5)

sha1_file -- ファイルの sha1 ハッシュを計算する

説明

string sha1_file ( string filename [, bool raw_output] )

US Secure Hash Algorithm 1 を使用して filename の sha1 ハッシュを計算し、そのハッシュを 返します。ハッシュは 40 文字の 16 進数となります。

パラメータ

filename

ファイル名。

raw_output

TRUE を指定すると、長さ 20 のバイナリフォーマットで ダイジェストを返します。デフォルトは FALSE です。

返り値

成功した場合に文字列、それ以外の場合に FALSE を返します。

変更履歴

バージョン説明
5.0.0 raw_output パラメータが追加されました。
5.1.0 ストリーム API を使用するように変更しました。つまり、 sha1_file('http://example.com/..') のように ラッパとともに使用できるということです。



add a note add a note User Contributed Notes
sha1_file
admin at cmsrevolution dot com
05-Aug-2006 12:02
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);
?>
gubatron at gmail dot com
11-Feb-2006 12:02
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

}

sha1" width="11" height="7"/> <setlocale
Last updated: Wed, 01 Nov 2006
 
 
show source | credits | sitemap | contact | advertising | mirror sites