sha1_file() - 计算文件的 sha1 散列值 - php 字符串函数
sha1_file()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
计算文件的 sha1 散列值
说明
sha1_file(string $filename[,bool $raw_output=FALSE
] ): string利用»美国安全散列算法 1,计算并返回由$filename指定的文件的 sha1 散列值。该散列值是一个 40 字符长度的十六进制数字。
参数
$filename要散列的文件的文件名。
$raw_output如果被设置为TRUE
,sha1 摘要将以 20 字符长度的原始格式返回。
返回值
成功返回一个字符串,否则返回FALSE
。
范例
Example #1sha1_file()范例
更新日志
版本 | 说明 |
---|---|
5.1.0 | 改变函数以使用流 API。这意味着可以使用包装器,比如sha1_file('http://example.com/..')。 |
参见
sha1()
计算字符串的 sha1 散列值md5_file()
计算指定文件的 MD5 散列值crc32()
计算一个字符串的 crc32 多项式
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).
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 $storedBitCount]; $storedData &= ((1 0) { $storedData
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)