Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Last active October 2, 2015 09:18
Show Gist options
  • Save dongilbert/2218183 to your computer and use it in GitHub Desktop.
Save dongilbert/2218183 to your computer and use it in GitHub Desktop.
This script outputs an array of file hashes recursively for the current directory. Useful for generating hashlist for security scanning programs.
<?php
/**
* This script outputs an array of file hashes recursively for the current directory.
* Useful for generating hashlist for security scanning programs
*
*/
function recursive_md5($dir, $types = null, $recursive = true, $baseDir = '')
{
$to_ignore = array(
'.',
'..',
'.DS_Store',
'.localized',
'create_file_hashes.php'
);
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if (in_array($file, $to_ignore))
{
continue;
}
if (is_file($dir . $file))
{
if (is_array($types))
{
if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true))
{
continue;
}
}
$md5sum = md5_file($dir . $file);
echo "\t" ."'{$baseDir}{$file}' => '{$md5sum}'," . PHP_EOL;
}
elseif($recursive && is_dir($dir . $file))
{
recursive_md5($dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR);
}
}
closedir($dh);
}
}
echo '<?php' . PHP_EOL;
echo '$filehashes = array(' . PHP_EOL;
recursive_md5(dirname(__FILE__) . DIRECTORY_SEPARATOR);
echo ');';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment