ParagonIE\Halite\Stream\ReadOnlyFile::getHash PHP Method

getHash() public method

Calculate a BLAKE2b hash of a file
public getHash ( ) : string
return string
    public function getHash() : string
    {
        $init = $this->pos;
        \fseek($this->fp, 0, SEEK_SET);
        // Create a hash context:
        $h = \Sodium\crypto_generichash_init($this->hashKey, \Sodium\CRYPTO_GENERICHASH_BYTES_MAX);
        for ($i = 0; $i < $this->stat['size']; $i += self::CHUNK) {
            if ($i + self::CHUNK > $this->stat['size']) {
                $c = \fread($this->fp, $this->stat['size'] - $i);
            } else {
                $c = \fread($this->fp, self::CHUNK);
            }
            \Sodium\crypto_generichash_update($h, $c);
        }
        // Reset the file pointer's internal cursor to where it was:
        \fseek($this->fp, $init, SEEK_SET);
        return \Sodium\crypto_generichash_final($h);
    }

Usage Example

Beispiel #1
0
 public function testFileHash()
 {
     $filename = \tempnam('/tmp', 'x');
     $buf = \Sodium\randombytes_buf(65537);
     \file_put_contents($filename, $buf);
     $fileOne = new ReadOnlyFile($filename);
     $fp = \fopen($filename, 'rb');
     $fileTwo = new ReadOnlyFile($fp);
     $this->assertSame($fileOne->getHash(), $fileTwo->getHash());
     \fclose($fp);
 }
All Usage Examples Of ParagonIE\Halite\Stream\ReadOnlyFile::getHash