checksum() public static method

Calculate the BLAKE2b-512 checksum of a file. This method doesn't load the entire file into memory. You may optionally supply a key to use in the BLAKE2b hash.
public static checksum ( string | resource $filePath, Key $key = null, boolean $raw = false ) : string
$filePath string | resource The file
$key Key (optional; expects SignaturePublicKey or AuthenticationKey)
$raw boolean Defaults to returning a hexadecimal string.
return string The checksum
Example #1
0
 /**
  * UpdateFile constructor.
  * @param array $data
  */
 public function __construct(array $data)
 {
     $this->path = $data['path'];
     $this->version = $data['version'];
     $this->size = (int) ($data['size'] ?? \filesize($data['path']));
     $this->hash = File::checksum($data['path']);
 }
Example #2
0
 /**
  * InstallFile constructor.
  *
  * @param Supplier $supplier
  * @param array $data
  */
 public function __construct(Supplier $supplier, array $data)
 {
     $this->hash = File::checksum($data['path']);
     $this->releaseInfo = $data['data']['releaseinfo'];
     $this->root = $data['data']['merkle_root'];
     $this->path = $data['path'];
     $this->size = (int) ($data['size'] ?? \filesize($data['path']));
     $this->supplier = $supplier;
     $this->version = $data['version'];
 }
Example #3
0
 /**
  * @covers File::checksum()
  */
 public function testChecksum()
 {
     $csum = File::checksum(__DIR__ . '/tmp/paragon_avatar.png');
     $this->assertEquals($csum, "09f9f74a0e742d057ca08394db4c2e444be88c0c94fe9a914c3d3758c7eccafb" . "8dd286e3d6bc37f353e76c0c5aa2036d978ca28ffaccfa59f5dc1f076c5517a0");
     $data = \Sodium\randombytes_buf(32);
     \file_put_contents(__DIR__ . '/tmp/garbage.dat', $data);
     $hash = \Sodium\crypto_generichash($data, null, 64);
     $file = File::checksum(__DIR__ . '/tmp/garbage.dat', null, true);
     $this->assertEquals($hash, $file);
     \unlink(__DIR__ . '/tmp/garbage.dat');
 }
Example #4
0
 /**
  * Process an upload. Either it returns an array with useful data,
  * OR it throws an UploadError
  *
  * @param int|null $directoryId
  * @param string $cabin
  * @param array $file
  * @param array $attribution Who uploaded it?
  * @return array
  * @throws UploadError
  */
 public function processUpload($directoryId = null, string $cabin = '', array $file = [], array $attribution = []) : array
 {
     // First step: Validate our file data
     switch ($file['error']) {
         case UPLOAD_ERR_INI_SIZE:
         case UPLOAD_ERR_FORM_SIZE:
             throw new UploadError(\__('File is too large'));
         case UPLOAD_ERR_PARTIAL:
             throw new UploadError(\__('Partial file received'));
         case UPLOAD_ERR_NO_TMP_DIR:
             throw new UploadError(\__('Temporary directory does not exist'));
         case UPLOAD_ERR_CANT_WRITE:
             throw new UploadError(\__('Cannot write to temporary directory'));
         case UPLOAD_ERR_OK:
             // Continue
             break;
     }
     if ($file['name'] === '..') {
         throw new UploadError(\__('Invalid file name'));
     }
     if (\preg_match('#([^/]+)\\.([a-zA-Z0-9]+)$#', $file['name'], $matches)) {
         $name = $matches[1];
         $ext = $matches[2];
     } elseif (\preg_match('#([^/\\.]+)$#', $file['name'], $matches)) {
         $name = $matches[1];
         $ext = 'txt';
     } else {
         throw new UploadError(\__('Invalid file name'));
     }
     // Actually upload the file.
     $destination = $this->moveUploadedFile($file['tmp_name']);
     $fullpath = AIRSHIP_UPLOADS . $destination;
     // Get the MIME type and checksum
     $type = $this->getMimeType($fullpath);
     $state = State::instance();
     $checksum = HaliteFile::checksum($fullpath, $state->keyring['cache.hash_key']);
     // Begin transaction
     $this->db->beginTransaction();
     // Get a unique file name
     $filename = $this->getUniqueFileName($name, $ext, $directoryId, $cabin);
     // Insert the new record
     $store = ['filename' => $filename, 'type' => $type, 'realname' => $destination, 'checksum' => $checksum, 'uploaded_by' => (int) ($attribution['uploaded_by'] ?? $this->getActiveUserId())];
     if ($directoryId) {
         $store['directory'] = (int) $directoryId;
     } else {
         $store['cabin'] = (string) $cabin;
     }
     if (!empty($attribution['author'])) {
         $store['author'] = $attribution['author'];
     }
     $newId = $this->db->insertGet('airship_files', $store, 'fileid');
     // Did our INSERT query fail?
     if (!$this->db->commit()) {
         // Clean up orphaned file, it was a database error.
         \unlink($fullpath);
         $this->db->rollBack();
         throw new UploadError(\__('A database error occurred trying to save %s', 'default', $destination));
     }
     // Return metadata
     return ['fileid' => $newId, 'name' => $filename, 'type' => $type, 'csum' => $checksum];
 }
Example #5
0
<?php

declare (strict_types=1);
require_once \dirname(__DIR__) . '/vendor/autoload.php';
/**
 * Generate a BLAKE2b-512 checksum of a given file.
 */
if (isset($argv[1])) {
    echo \ParagonIE\Halite\File::checksum($argv[1]), "\n";
}