/** * 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']); }
public function testChecksum() { $csum = File::checksumFile(__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::checksumFile(__DIR__ . '/tmp/garbage.dat', null, true); $this->assertEquals($hash, $file); }
public function testSeal() { \touch(__DIR__ . '/tmp/paragon_avatar.sealed.png'); \chmod(__DIR__ . '/tmp/paragon_avatar.sealed.png', 0777); \touch(__DIR__ . '/tmp/paragon_avatar.opened.png'); \chmod(__DIR__ . '/tmp/paragon_avatar.opened.png', 0777); list($secretkey, $publickey) = Key::generate(Key::CRYPTO_BOX); File::sealFile(__DIR__ . '/tmp/paragon_avatar.png', __DIR__ . '/tmp/paragon_avatar.sealed.png', $publickey); File::unsealFile(__DIR__ . '/tmp/paragon_avatar.sealed.png', __DIR__ . '/tmp/paragon_avatar.opened.png', $secretkey); $this->assertEquals(\hash_file('sha256', __DIR__ . '/tmp/paragon_avatar.png'), \hash_file('sha256', __DIR__ . '/tmp/paragon_avatar.opened.png')); }
/** * Check that the signature is valid for this supplier's * public keys. * * @param bool $fastExit * @return bool */ public function signatureIsValid(bool $fastExit = false) : bool { $result = false; foreach ($this->supplier->getSigningKeys() as $key) { $result = $result || File::verify($this->path, $key['key'], $this->releaseInfo['signature']); if ($result && $fastExit) { return true; } } return $result; }
/** * 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]; }
<?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"; }