Tuesday, June 26, 2012

Alternate Hash Algorithms for ImageMagick's GetImageSignature in PHP

The getImageSignature function for the ImageMagick package in PHP returns only the SHA-256 hash value for an image.  There are not any other algorithms available for it within the ImageMagick package, but fortunately, this is PHP and you have a wide array of hashing algorithms to use on any image file.  The function hash_algos() will return an array of hashing algorithms available in PHP and the function hash_file() will take three parameters (one for the algorithm to use, one for the filename, and an optional variable for binary output).  Instead of being limited to the SHA-256 algorithm of the getImageSignature function, you could use SHA-1, SHA-384, SHA-512, Whirlpool, HAVAL, Salsa, Gost, Adler32, CRC32, or MD5, among others and variations of these.

However, the SHA-256 result of the hash_file() function performed on a file does not return the same result as the SHA-256 of the getImageSignature() function.  This leads me to believe that the SHA-256 of the getImageSignature() might be performed on the Imagick object itself, instead of the file, whereas the hash_file() function is clearly performed on the file itself.  That's just a guess, though.

Some sample code for the alternate hashing algorithm, with every algorithm performed on the image file :

<?php

            // Author: holdoffhunger@gmail.com

        // Set File for Hashing Function
        // ---------------------------------------------------

    $filename_with_folder_for_hashing = "image_workshop/test_file.bmp";
   
        // Acquire Dynamic Hashing Algorithm List
        // ---------------------------------------------------

    $php_dynamic_hash_algorithms = hash_algos();

        // Count Dynamic Hashing Algorithm List
        // ---------------------------------------------------
   
    $number_of_dynamic_hash_algorithms = count($php_dynamic_hash_algorithms);

        // Parse Dynamic Hashing Algorithm List
        // ---------------------------------------------------
   
    for($i = 0; $i < $number_of_dynamic_hash_algorithms; $i++)
    {
            // Get Current Hashing Algorithm
            // ---------------------------------------------------

        $current_hashing_algorithm = $php_dynamic_hash_algorithms[$i];

            // Perform Hashing on File
            // ---------------------------------------------------

        $current_hashing_algorithm_hex_result = hash_file($current_hashing_algorithm, $filename_with_folder_for_hashing, FALSE);

            // Print Hashed Results
            // ---------------------------------------------------

        print("$current_hashing_algorithm Algorithm ::: $current_hashing_algorithm_hex_result");
        print("<br><br>");
    }

?>

    Example Results:
    ...........................

md2 Algorithm ::: 03205df9c6717d74f1f003c66f58e98a

md4 Algorithm ::: b2a204a2e5c3968d2abd5dc372fbee10

md5 Algorithm ::: b9d92a61714b221d24c7730d4764ca82

sha1 Algorithm ::: 8db4c92346c26568b13ea43fbcf514e37942a41a

(and so on for 34 more algorithms)...

Official Function Page: http://www.php.net/manual/en/imagick.getimagesignature.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

No comments:

Post a Comment