php sha1 长度,PHP: sha1_file - Manual

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值