File Integrity Hash Review
Compares expected SHA-256 hashes against current file hashes and reports changed, missing, and clean files.
Purpose
Compares expected SHA-256 hashes against current file hashes and reports changed, missing, and clean files.
Snippet details
ContextFileLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* File Integrity Hash Review.
*
* Purpose:
* Compares expected SHA-256 hashes against current file hashes and reports changed, missing, and clean files.
*
* @param array $expected_hashes Expected SHA-256 hashes keyed by file path.
* @return array Hash review rows.
*/
function ogSnippetFileIntegrityHashReview(array $expected_hashes): array {
$rows = array();
foreach ($expected_hashes as $path => $expected_hash) {
$status = 'missing';
$current_hash = '';
if (is_file((string) $path)) {
$current_hash = hash_file('sha256', (string) $path);
$status = 'changed';
if (hash_equals((string) $expected_hash, $current_hash)) {
$status = 'clean';
}
}
$rows[] = array('path' => (string) $path, 'expected' => (string) $expected_hash, 'current' => $current_hash, 'status' => $status);
}
return $rows;
}
$integrity_rows = ogSnippetFileIntegrityHashReview(array(__FILE__ => hash_file('sha256', __FILE__)));
echo $integrity_rows[0]['status'];