文字

sha1_file

(PHP 4 >= 4.3.0, PHP 5)

sha1_file计算文件的 sha1 散列值

说明

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

利用» 美国安全散列算法 1,计算并返回由 filename 指定的文件的 sha1 散列值。该散列值是一个 40 字符长度的十六进制数字。

参数

filename

要散列的文件的文件名。

raw_output

如果被设置为 TRUE ,sha1 摘要将以 20 字符长度的原始格式返回。

返回值

成功返回一个字符串,否则返回 FALSE

范例

Example #1 sha1_file() 范例

<?php
foreach( glob ( '/home/Kalle/myproject
class SHA1 {
  static $BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

  
  static function fileSHA1($file) {
    $raw = sha1_file($file,true);
    return SHA1::base32encode($raw);
  } //fileSHA1

  
  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

}
上一篇: 下一篇: