There is a big disclaimer that should be made to all those about to venture on ImageMagick programming using PHP: the great, vast majority of WAMP (Windows-Apache-MySQL-PHP) servers do not support ImageMagick commands at all, since it is a "different package," and there appears to be no way to install ImageMagick through standard Add/Remove Applications within Linux (as far as standard Debian linux releases go). It's very easy to get Apache/PHP running in either Windows or Linux with pretty much any standard OS/distro supported so that you can run all of your own code on your own computer. But that's definitely not the case when you want Imagick to cooperate with your Apache server and your PHP code!
It took me almost an entire week just to get Apache/PHP/ImageMagick working all together nice and happily so that I could run my own PHP code on my own machine (natively, too). So, I decided to simply write down the steps, to help anyone else out who could need help.
Guide to Building an Apache/PHP/Imagick Server with Linux/CentOS 6.4:::
1. Install CentOS 6.4. (Theoretically, other versions of CentOS should work as well, but I simply chose the most recent one.) Also, you can very easily emulate CentOS within Windows (for instance, the unfree choice of VMWare).
2. Install PHP, PHP-Common, PHP-extensions, etc., from standard Application Package Manager ("Add/Remove Programs", usually in system tools somewhere). (You don't have to worry about Apache, because that comes with CentOS 6.4 anyway.)
3. Open up a terminal and enter admin mode with the command "su". You'll need a password for this.
4. Enter this command: yum install ImageMagick ImageMagick-devel
5. Enter this command: pecl install imagick
6. Modify php.ini and include the line "extension=imagick.so". This file is located at "/etc/php.ini" normally. The line must be exactly inserted in the section of "Dynamic Extensions", which begins with this commented out text:
--------------------------------
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
--------------------------------
Use the "search" feature of gedit to find the text. Add the extension below the commented out note in this section so that the result looks like:
--------------------------------
;;;;
; Note: packaged extension modules are now loaded via the .ini files
; found in the directory /etc/php.d; these are loaded by default.
;;;;
extension=imagick.so
--------------------------------
Putting the extension directly at the top caused me serious permission issues in running Imagick code. Also, with CentOS, the permissions automatically disable user control for this php.ini. Enter the following terminal command: "chown [YOUR-USERNAME] -R /etc/php.ini", without the quotes to get control to modify it. Note to insert your username where it states "[YOUR-USERNAME]".
7. Enter this command: "service httpd restart", without quotes, from admin mode. This restarts Apache/PHP/PHP's extensions now that you've installed ImageMagick. If this gives you an error, just try "service httpd start".
My favorite part about this method is that you can update your system with issuing the "yum update" command and then the "yum upgrade" command -- and the Apache/PHP/Imagick combination will still be working perfectly! Unlike some other guides out there for this, you don't need to rely on antiquated, unsupported versions that can be nearly impossible to find. Also, you don't need to worry about the MySQL, Hash, MBString, Exif packages, because they all seem to be installed and properly cooperating with PHP automatically after installing PHP from the Application Manager. This solution makes everything work well together.
The root directory of your public files is located at /var/www/html/. Normally, this folder is not owned by the user, so you'll get a permission error when trying to add files or folders to it. Correct that with entering admin mode in the terminal/command-line window ("su" command) and entering the following command: "chown [YOUR-USERNAME] -R /var/www/html/", without quotes. Again, replace "[YOUR-USERNAME]" with your actual user name. (Warning: The first time I did this, I was lazy and entered the command for only the "/var/" folder, which prevented my system ever from booting. It would always freeze with the notorious and googleable error-message: "Could not update ICEauthority file /var/lib/gdm/.ICEauthority". So, make sure to change ownership only for "/var/www/html/".)
To view your root directory files as compiled, resultant, PHP pages, open up a browser and enter "127.0.0.1" as the address. So, a file in your root directory would be accessed at the following URL in your browser: http://127.0.0.1/your.file.php . If you get a "could not connect" error message, try the command "service httpd start" at the terminal window from admin mode. (You have to start this service manually every time the machine boots, although there's plenty of ways to automate it.)
I hope this helps! I tried a hundred different guides on getting Apache/PHP/Imagick to work together and none of them worked. Don't give up! ImageMagick is a package of really neat imaging functions!
Open Software Coders of the World, You Have Nothing to Lose but Your Proprietary Chains! (...delivering advice for users of PHP/Linux/Apache)
Thursday, May 30, 2013
Monday, July 2, 2012
Image Rendering Intent in the ImageMagick Package of PHP
The Rendering Intent variable exists in ImageMagick to provide support for ICC Color Profiles. There are five results that one can expect to get from the getRenderingIntent function, which are the predefined constants for Rendering Intent within ImageMagick. These values look like "imagick::RENDERINGINTENT_UNDEFINED", with "_VALUE" values of: undefined, saturation, perceptual, absolute, and relative. Printed out, Undefined is 0, Saturation is 1, Perceptual is 2, Absolute is 3, and Relative is 4.
The official ImageMagick documentation provides good light on what these values indicate. ( http://www.imagemagick.org/RMagick/doc/constants.html#RenderingIntent ) Undefined means "no intent," Saturation means "A rendering intent that specifies the saturation of the pixels in the image is preserved perhaps at the expense of accuracy in hue and lightness," Perceptual Intent means "A rendering intent that specifies the full gamut of the image is compressed or expanded to fill the gamut of the destination device. Gray balance is preserved but colorimetric accuracy might not be preserved," Absolute Intent means "Absolute colorimetric," and Relative Intent means "Relative colorimetric."
Every time I ran this function, it always produced the same result: #2, indicating Perceptual Intent. But this makes sense, since the ImageMagick documentation notes about the Rendering Intent, "Typically, the user or application will set the rendering intent dynamically at runtime or embedding time." The ImageMagick documentation also recommended the following link to better understand Rendering Intent in terms of Colorspace Profiles: http://www.cambridgeincolour.com/tutorials/color-space-conversion.htm .
Some sample code :
<?php
// Author: holdoffhunger@gmail.com
// Imagick Type
// ---------------------------------------------
$imagick_type = new Imagick();
// Open File
// ---------------------------------------------
$file_to_grab = "image_workshop_directory/test.jpg";
$file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
// Grab File
// ---------------------------------------------
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// Get Rendering Intent Values
// ---------------------------------------------
$imagick_rendering_intent = $imagick_type->getImageRenderingIntent();
switch($imagick_rendering_intent)
{
case '0':
$image_rendering_intent_evaluated = "Undefined";
break;
case '1':
$image_rendering_intent_evaluated = "Saturation";
break;
case '2':
$image_rendering_intent_evaluated = "Perceptual";
break;
case '3':
$image_rendering_intent_evaluated = "Absolute";
break;
case '4':
$image_rendering_intent_evaluated = "Relative";
break;
}
// Print Rendering Intent Values
// ---------------------------------------------
print("# $imagick_rendering_intent - $image_rendering_intent_evaluated");
?>
Official Function Page: http://www.php.net/manual/en/imagick.getimagerenderingintent.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
The official ImageMagick documentation provides good light on what these values indicate. ( http://www.imagemagick.org/RMagick/doc/constants.html#RenderingIntent ) Undefined means "no intent," Saturation means "A rendering intent that specifies the saturation of the pixels in the image is preserved perhaps at the expense of accuracy in hue and lightness," Perceptual Intent means "A rendering intent that specifies the full gamut of the image is compressed or expanded to fill the gamut of the destination device. Gray balance is preserved but colorimetric accuracy might not be preserved," Absolute Intent means "Absolute colorimetric," and Relative Intent means "Relative colorimetric."
Every time I ran this function, it always produced the same result: #2, indicating Perceptual Intent. But this makes sense, since the ImageMagick documentation notes about the Rendering Intent, "Typically, the user or application will set the rendering intent dynamically at runtime or embedding time." The ImageMagick documentation also recommended the following link to better understand Rendering Intent in terms of Colorspace Profiles: http://www.cambridgeincolour.com/tutorials/color-space-conversion.htm .
Some sample code :
<?php
// Author: holdoffhunger@gmail.com
// Imagick Type
// ---------------------------------------------
$imagick_type = new Imagick();
// Open File
// ---------------------------------------------
$file_to_grab = "image_workshop_directory/test.jpg";
$file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
// Grab File
// ---------------------------------------------
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// Get Rendering Intent Values
// ---------------------------------------------
$imagick_rendering_intent = $imagick_type->getImageRenderingIntent();
switch($imagick_rendering_intent)
{
case '0':
$image_rendering_intent_evaluated = "Undefined";
break;
case '1':
$image_rendering_intent_evaluated = "Saturation";
break;
case '2':
$image_rendering_intent_evaluated = "Perceptual";
break;
case '3':
$image_rendering_intent_evaluated = "Absolute";
break;
case '4':
$image_rendering_intent_evaluated = "Relative";
break;
}
// Print Rendering Intent Values
// ---------------------------------------------
print("# $imagick_rendering_intent - $image_rendering_intent_evaluated");
?>
Official Function Page: http://www.php.net/manual/en/imagick.getimagerenderingintent.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
Sunday, July 1, 2012
Image Orientation in the ImageMagick Package of PHP
Using the getImageOrientation function, you'll get the Orientation value for an image as defined within the EXIF file format. That means you will get back an integer representation one of the Orientation constants for ImageMagick, which looks like " imagick::ORIENTATION_UNDEFINED", with "_VALUE" values of: undefined (0), topleft (1), topright (2), bottomright (3), bottomleft (4), lefttop (5), righttop (6), rightbottom (7), and leftbottom (8). When printed out directly, these predefined constants produce the number in parenthesis. The undefined being set to 0 makes sense, since -- according to Wikipedia -- EXIF allows for eight possible values for an image (and not every image has a set of EXIF properties).
The EXIF Orientation is also called "the Rotation," again, according to Wikipedia: http://en.wikipedia.org/wiki/Exchangeable_image_file_format . What's the point of that? According to the official ImageMagick documentation, it's to rotate a photograph so that it is properly oriented after the shot. This really seems to be a camera thing, since the ImageMagick documentation mentions: "If you pointed your camera almost straight up or down, the EXIF orientation setting may not resolve correctly. The same goes for angled or slanted shots. The orientation (and cameras) just have no senses for these situations." ( http://www.imagemagick.org/Usage/photos/ )
If you're having trouble getting this function to show the value for Image Orientation, then use the function and parameters of getImageProperties('*', FALSE); . This produces an array of all properties associated with an image, and one of them will have the key value of exif:Orientation. If it doesn't, then that means you will be getting back a zero from this function, indicating an "Undefined" orientation.
Some sample code :
<?php
// Author: holdoffhunger@gmail.com
// Imagick Type
// ---------------------------------------------
$imagick_type = new Imagick();
// Open File
// ---------------------------------------------
$file_to_grab = "image_workshop_directory/test.jpg";
$file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
// Grab File
// ---------------------------------------------
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// Get Orientation Values
// ---------------------------------------------
$imagick_orientation = $imagick_type->getImageOrientation();
switch($imagick_orientation)
{
case '0':
$imagick_orientation_evaluated = "Undefined";
break;
case '1':
$imagick_orientation_evaluated = "Top-Left";
break;
case '2':
$imagick_orientation_evaluated = "Top-Right";
break;
case '3':
$imagick_orientation_evaluated = "Bottom-Right";
break;
case '4':
$imagick_orientation_evaluated = "Bottom-Left";
break;
case '5':
$imagick_orientation_evaluated = "Left-Top";
break;
case '6':
$imagick_orientation_evaluated = "Right-Top";
break;
case '7':
$imagick_orientation_evaluated = "Right-Bottom";
break;
case '8':
$imagick_orientation_evaluated = "Left-Bottom";
break;
}
// Print Orientation Values
// ---------------------------------------------
print("# $imagick_orientation - $imagick_orientation_evaluated");
?>
Official Function Page: http://www.php.net/manual/en/imagick.getimageorientation.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
The EXIF Orientation is also called "the Rotation," again, according to Wikipedia: http://en.wikipedia.org/wiki/Exchangeable_image_file_format . What's the point of that? According to the official ImageMagick documentation, it's to rotate a photograph so that it is properly oriented after the shot. This really seems to be a camera thing, since the ImageMagick documentation mentions: "If you pointed your camera almost straight up or down, the EXIF orientation setting may not resolve correctly. The same goes for angled or slanted shots. The orientation (and cameras) just have no senses for these situations." ( http://www.imagemagick.org/Usage/photos/ )
If you're having trouble getting this function to show the value for Image Orientation, then use the function and parameters of getImageProperties('*', FALSE); . This produces an array of all properties associated with an image, and one of them will have the key value of exif:Orientation. If it doesn't, then that means you will be getting back a zero from this function, indicating an "Undefined" orientation.
Some sample code :
<?php
// Author: holdoffhunger@gmail.com
// Imagick Type
// ---------------------------------------------
$imagick_type = new Imagick();
// Open File
// ---------------------------------------------
$file_to_grab = "image_workshop_directory/test.jpg";
$file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
// Grab File
// ---------------------------------------------
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// Get Orientation Values
// ---------------------------------------------
$imagick_orientation = $imagick_type->getImageOrientation();
switch($imagick_orientation)
{
case '0':
$imagick_orientation_evaluated = "Undefined";
break;
case '1':
$imagick_orientation_evaluated = "Top-Left";
break;
case '2':
$imagick_orientation_evaluated = "Top-Right";
break;
case '3':
$imagick_orientation_evaluated = "Bottom-Right";
break;
case '4':
$imagick_orientation_evaluated = "Bottom-Left";
break;
case '5':
$imagick_orientation_evaluated = "Left-Top";
break;
case '6':
$imagick_orientation_evaluated = "Right-Top";
break;
case '7':
$imagick_orientation_evaluated = "Right-Bottom";
break;
case '8':
$imagick_orientation_evaluated = "Left-Bottom";
break;
}
// Print Orientation Values
// ---------------------------------------------
print("# $imagick_orientation - $imagick_orientation_evaluated");
?>
Official Function Page: http://www.php.net/manual/en/imagick.getimageorientation.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
Saturday, June 30, 2012
Difference Between the Hash() and Hash_File() Functions in PHP
The Hash_File() function returns the same value as if the function Hash() had been performed on the same exact piece of data. At first, I was uncertain if Hash_File() used the filename, or even the permission settings, when defining the data to be hashed for the given algorithm. If it did work that way, then that means the same exact files would have different HASH values when you moved or renamed them on your system. Anyway, fortunately, it does not work that way. Hash() and Hash_File() produce identical results for the same pieces of data. This is also true for the relationship between the Hash_HMAC() and Hash_HMAC_File() functions: the same pieces of data, the same keys, produce identical results. It was a wise, design principle.
Some sample code to demonstrate this principle :
<?php
// Author: holdoffhunger@gmail.com
// Preset Data
// ------------------------------------------------
$test_data = "php-hashing";
$test_file = "test.txt";
$test_file_read = file_get_contents($test_file);
// Hash Data
// ------------------------------------------------
$test_data_hash = hash("md2", $test_data, FALSE);
$test_file_hash = hash_file("md2", $test_file, FALSE);
// Print Hash Results
// ------------------------------------------------
print("Data Hash ($test_data): $test_data_hash<br><br>");
print("File Hash ($test_file_read): $test_file_hash");
?>
Expected Results
..................................
Data Hash (php-hashing): 457d84e1d69e519a7b73348db21477d3
File Hash (php-hashing): 457d84e1d69e519a7b73348db21477d3
Official Function Page: http://www.php.net/manual/en/function.hash-file.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
Some sample code to demonstrate this principle :
<?php
// Author: holdoffhunger@gmail.com
// Preset Data
// ------------------------------------------------
$test_data = "php-hashing";
$test_file = "test.txt";
$test_file_read = file_get_contents($test_file);
// Hash Data
// ------------------------------------------------
$test_data_hash = hash("md2", $test_data, FALSE);
$test_file_hash = hash_file("md2", $test_file, FALSE);
// Print Hash Results
// ------------------------------------------------
print("Data Hash ($test_data): $test_data_hash<br><br>");
print("File Hash ($test_file_read): $test_file_hash");
?>
Expected Results
..................................
Data Hash (php-hashing): 457d84e1d69e519a7b73348db21477d3
File Hash (php-hashing): 457d84e1d69e519a7b73348db21477d3
Official Function Page: http://www.php.net/manual/en/function.hash-file.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
Friday, June 29, 2012
Using the MHash Function to Create HMAC Hash Results in PHP
The MHash function here lists one disclaimer at the top when providing a key to the mhash function : "Not all algorithms supported in mhash can be used in HMAC mode." So, what algorithms blow up and what do fine when it comes to doing keyed, HMAC hashing? The destructive ones are: Adler32, CRC32, CRC32B, and GOST. These are the first four, predefined constants listed with the MHash Application Package: http://www.php.net/manual/en/mhash.constants.php . Providing one of these algorithms with an HMAC key (of string-length greater than one) creates the following error message: "Warning: mhash() [function.mhash]: mhash initialization failed in [(folder-location)] on line 181". If you really want to use these algorithms in creating your HMAC hashes, the function Hash_hmac() from the HASH-Message Digest Framework package is capable of doing that perfectly.
Again, that is if the string length is greater than one. Why string length greater than one? Well, if the key value is blank, it is ignored as a parameter altogether. So, if you feed the mhash function an algorithm that is not compatible with HMAC hashing and an HMAC key that's blank (""), it will work the same as if it had received no HMAC key at all. This is different from the way the Hash_hmac() function of the HASH-MDF works. In the case of the Hash_hmac() function, feeding a blank HMAC key will use that blank key in generating the HMAC hash. Even with functions that can do HMAC hashing, like MD5 or SHA1, if the MHash() is given a blank HMAC key, it will ignore the key and just return the results of standard, non-HMAC hashing. It's probably not wise to use a blank HMAC key anyway, but it's good to know that the hashing algorithm changes altogether if the provided HMAC key is blank.
Some sample code to demonstrate :
<?php
// Author: holdoffhunger@gmail.com
// Preset Data
// ---------------------------------------------------
$string_to_hash = "The hash_hmac() function better to use for these purposes.";
$blank_hmac_key = "";
// MHash - Hashing With and Without HMAC Parameter
// ---------------------------------------------------
$mhash_result_with_hmac_parameter = bin2hex(mhash(MHASH_CRC32, $string_to_hash, $blank_hmac_key));
$mhash_result_without_hmac_parameter = bin2hex(mhash(MHASH_CRC32, $string_to_hash));
// MHash - Hashing With and Without HMAC Parameter
// ---------------------------------------------------
$hash_result_with_hmac_parameter = hash_hmac("crc32", $string_to_hash, $blank_hmac_key);
$hash_result_without_hmac_parameter = hash("crc32", $string_to_hash);
// Print Results
// ---------------------------------------------------
print("MHASH (CRC32 Algorithm) With Blank HMAC Key: $mhash_result_with_hmac_parameter .<br>");
print("MHASH (CRC32 Algorithm) Without HMAC Processing: $mhash_result_without_hmac_parameter .<br><br>");
print("HASH (CRC32 Algorithm) With Blank HMAC Key: $hash_result_with_hmac_parameter .<br>");
print("HASH (CRC32 Algorithm) Without HMAC Processing: $hash_result_without_hmac_parameter .");
?>
Results :
...................................
MHASH (CRC32 Algorithm) With Blank HMAC Key: f665c094 .
MHASH (CRC32 Algorithm) Without HMAC Processing: f665c094 .
HASH (CRC32 Algorithm) With Blank HMAC Key: 3041f4f8 .
HASH (CRC32 Algorithm) Without HMAC Processing: f665c094 .
Official Function Page: http://www.php.net/manual/en/function.mhash.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
Again, that is if the string length is greater than one. Why string length greater than one? Well, if the key value is blank, it is ignored as a parameter altogether. So, if you feed the mhash function an algorithm that is not compatible with HMAC hashing and an HMAC key that's blank (""), it will work the same as if it had received no HMAC key at all. This is different from the way the Hash_hmac() function of the HASH-MDF works. In the case of the Hash_hmac() function, feeding a blank HMAC key will use that blank key in generating the HMAC hash. Even with functions that can do HMAC hashing, like MD5 or SHA1, if the MHash() is given a blank HMAC key, it will ignore the key and just return the results of standard, non-HMAC hashing. It's probably not wise to use a blank HMAC key anyway, but it's good to know that the hashing algorithm changes altogether if the provided HMAC key is blank.
Some sample code to demonstrate :
<?php
// Author: holdoffhunger@gmail.com
// Preset Data
// ---------------------------------------------------
$string_to_hash = "The hash_hmac() function better to use for these purposes.";
$blank_hmac_key = "";
// MHash - Hashing With and Without HMAC Parameter
// ---------------------------------------------------
$mhash_result_with_hmac_parameter = bin2hex(mhash(MHASH_CRC32, $string_to_hash, $blank_hmac_key));
$mhash_result_without_hmac_parameter = bin2hex(mhash(MHASH_CRC32, $string_to_hash));
// MHash - Hashing With and Without HMAC Parameter
// ---------------------------------------------------
$hash_result_with_hmac_parameter = hash_hmac("crc32", $string_to_hash, $blank_hmac_key);
$hash_result_without_hmac_parameter = hash("crc32", $string_to_hash);
// Print Results
// ---------------------------------------------------
print("MHASH (CRC32 Algorithm) With Blank HMAC Key: $mhash_result_with_hmac_parameter .<br>");
print("MHASH (CRC32 Algorithm) Without HMAC Processing: $mhash_result_without_hmac_parameter .<br><br>");
print("HASH (CRC32 Algorithm) With Blank HMAC Key: $hash_result_with_hmac_parameter .<br>");
print("HASH (CRC32 Algorithm) Without HMAC Processing: $hash_result_without_hmac_parameter .");
?>
Results :
...................................
MHASH (CRC32 Algorithm) With Blank HMAC Key: f665c094 .
MHASH (CRC32 Algorithm) Without HMAC Processing: f665c094 .
HASH (CRC32 Algorithm) With Blank HMAC Key: 3041f4f8 .
HASH (CRC32 Algorithm) Without HMAC Processing: f665c094 .
Official Function Page: http://www.php.net/manual/en/function.mhash.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
Thursday, June 28, 2012
Parsing the Algorithms Available in MHash Within PHP
The MHash function mhash_count() returns the highest, evaluated, constant value representing a hashing algorithm available within the current MHash install. For example, 0 indicates CRC32, 1 indicates MD5, 2 indicates SHA1, etc., etc.. You can get the evaluated number for any of these algorithms by doing a print statement with any of the predefined, MHash constants, such as those available here : http://www.php.net/manual/en/mhash.constants.php .
The purpose of mhash_count() in MHash is similar to the purpose of hash_algos() in HASH Message Digest Framework. Instead of providing an array of available hashing algorithms, it simply provides the highest, evaluated, constant expression for the Algorithm Constants. The thinking, as indicated in the sample code, is that you will parse a list of mhash algorithms in a for loop using a condition of "$i <= mhash_count()".
However, there is a problem with that: there are several integers that are skipped in the listing of evaluated constant expressions. While 2 indicates SHA1, 3 indicates HAVAL256, etc., there is nothing for the numbers 4 and 6. They produce blank results when on that line of the for-loop and you're calling functions like mhash_get_block_size() and mhash_get_hash_name(). These were likely reserved for algorithms that were removed for one reason or another, either inefficiency or lack of security, and the number-to-predefined-constant setup wasn't changed to make it backward compatible with older code.
If you want to know what counts as a good constant and what doesn't, try running this code :
<?php
// Author: holdoffhunger@gmail.com
// Parse All Hashing Algorithms
// ---------------------------------------------------
for($i = 0; $i <= mhash_count(); $i++)
{
// Get Algorithm Information
// ---------------------------------------------------
$mhash_algorithm_block_size = mhash_get_block_size($i);
$mhash_algorithm_name = mhash_get_hash_name($i);
// Decide on Printing Algorithm Information
// ---------------------------------------------------
if(strlen($mhash_algorithm_name) < 1)
{
// There *IS NO* Available Algorithm Data
// ---------------------------------------------------
print("# $i --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .<br><br>");
}
else
{
// There *IS* Available Algorithm Data
// ---------------------------------------------------
print("# $i --- $mhash_algorithm_name to $mhash_algorithm_block_size .<br><br>");
}
}
?>
Sample Results
............................
# 0 --- CRC32 to 4 .
# 1 --- MD5 to 16 .
# 2 --- SHA1 to 20 .
# 3 --- HAVAL256 to 32 .
# 4 --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .
# 5 --- RIPEMD160 to 20 .
# 6 --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .
# 7 --- TIGER to 24 .
# 8 --- GOST to 32 .
...(and so on)...
Official Function Page: http://www.php.net/manual/en/function.mhash-count.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
The purpose of mhash_count() in MHash is similar to the purpose of hash_algos() in HASH Message Digest Framework. Instead of providing an array of available hashing algorithms, it simply provides the highest, evaluated, constant expression for the Algorithm Constants. The thinking, as indicated in the sample code, is that you will parse a list of mhash algorithms in a for loop using a condition of "$i <= mhash_count()".
However, there is a problem with that: there are several integers that are skipped in the listing of evaluated constant expressions. While 2 indicates SHA1, 3 indicates HAVAL256, etc., there is nothing for the numbers 4 and 6. They produce blank results when on that line of the for-loop and you're calling functions like mhash_get_block_size() and mhash_get_hash_name(). These were likely reserved for algorithms that were removed for one reason or another, either inefficiency or lack of security, and the number-to-predefined-constant setup wasn't changed to make it backward compatible with older code.
If you want to know what counts as a good constant and what doesn't, try running this code :
<?php
// Author: holdoffhunger@gmail.com
// Parse All Hashing Algorithms
// ---------------------------------------------------
for($i = 0; $i <= mhash_count(); $i++)
{
// Get Algorithm Information
// ---------------------------------------------------
$mhash_algorithm_block_size = mhash_get_block_size($i);
$mhash_algorithm_name = mhash_get_hash_name($i);
// Decide on Printing Algorithm Information
// ---------------------------------------------------
if(strlen($mhash_algorithm_name) < 1)
{
// There *IS NO* Available Algorithm Data
// ---------------------------------------------------
print("# $i --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .<br><br>");
}
else
{
// There *IS* Available Algorithm Data
// ---------------------------------------------------
print("# $i --- $mhash_algorithm_name to $mhash_algorithm_block_size .<br><br>");
}
}
?>
Sample Results
............................
# 0 --- CRC32 to 4 .
# 1 --- MD5 to 16 .
# 2 --- SHA1 to 20 .
# 3 --- HAVAL256 to 32 .
# 4 --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .
# 5 --- RIPEMD160 to 20 .
# 6 --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .
# 7 --- TIGER to 24 .
# 8 --- GOST to 32 .
...(and so on)...
Official Function Page: http://www.php.net/manual/en/function.mhash-count.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
Wednesday, June 27, 2012
MHash compared with HASH Message Digest Framework in PHP
Both cryptography packages, the MHash and the HASH Message Digest Framework, have the same algorithms, and yet, they both sometimes to produce wildly different results from each other when applying the same algorithm to the same piece of data. The SHA-x algorithms, as designed by the NSA, all seem to have concrete standards for producing their hash values, so they have similar results. Even the two MD5 implementations produce identical results, and the same goes for Gost, RipeMD, CRC32, Whirlpool, Snefru256 (known as 'Snefru256' in MHash and simply 'Snefru' in HASH-MDF), and Tiger (the three-round versions in HASH-MDF to simply 'Tigerx' in MHash).
However, the algorithms CRC32B and Adler32 each produce different results when called from either MHash or HASH-MDF, possibly because they are hashing algorithms designed to be checksums rather than something that can produce a string as a unique identifier for a particular piece of information. For that reason, if you ever publish the hash results with the data you're putting out publicly, it's probably wise to indicate whether it's the MHash or HASH-MDF implementation of the algorithm. Otherwise, the hash value won't provide much use as a unique identifier for the particular piece of data or for the file.
Some example code to better explain what I mean :
<?php
// Author: holdoffhunger@gmail.com
// SHA-1 Hashing
// ---------------------------------------------------
$mhash_sha1_results = bin2hex(mhash(MHASH_SHA1, "secret"));
$hash_mdf_sha1_results = hash("sha1", "secret", FALSE);
print("MHash SHA-1: $mhash_sha1_results .<br>");
print("HASH-MDF SHA-1: $hash_mdf_sha1_results .<br><br>");
// Whirlpool Hashing
// ---------------------------------------------------
$mhash_whirlpool_results = bin2hex(mhash(MHASH_WHIRLPOOL, "secret"));
$hash_mdf_whirlpool_results = hash("whirlpool", "secret", FALSE);
print("MHash Whirlpool: $mhash_whirlpool_results .<br>");
print("HASH-MDF Whirlpool: $hash_mdf_whirlpool_results .<br><br>");
// CRC32B Hashing
// ---------------------------------------------------
$mhash_crc32b_results = bin2hex(mhash(MHASH_CRC32B, "secret"));
$hash_mdf_crc32b_results = hash("crc32b", "secret", FALSE);
print("MHash CRC32B: $mhash_crc32b_results .<br>");
print("HASH-MDF CRC32B: $hash_mdf_crc32b_results .<br><br>");
// Adler32 Hashing
// ---------------------------------------------------
$mhash_adler32_results = bin2hex(mhash(MHASH_ADLER32, "secret"));
$hash_mdf_adler32_results = hash("adler32", "secret", FALSE);
print("MHash Adler32: $mhash_adler32_results .<br>");
print("HASH-MDF Adler32: $hash_mdf_adler32_results .<br><br>");
?>
Expected Results :
........................
MHash SHA-1: e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4 .
HASH-MDF SHA-1: e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4 .
MHash Whirlpool: e061b87a674ae3880e159ab55ed35d6c5e8a6aefac6ab08304a50588018d377b28639bb15fdeedf006d57e45f7b4298e6dfefceaf7c92c826a708fe6d1156eb3 .
HASH-MDF Whirlpool: e061b87a674ae3880e159ab55ed35d6c5e8a6aefac6ab08304a50588018d377b28639bb15fdeedf006d57e45f7b4298e6dfefceaf7c92c826a708fe6d1156eb3 .
MHash CRC32B: e5e8a25c .
HASH-MDF CRC32B: 5ca2e8e5 .
MHash Adler32: 8702d108 .
HASH-MDF Adler32: 08d10287 .
Official Function Page: http://www.php.net/manual/en/function.mhash.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
However, the algorithms CRC32B and Adler32 each produce different results when called from either MHash or HASH-MDF, possibly because they are hashing algorithms designed to be checksums rather than something that can produce a string as a unique identifier for a particular piece of information. For that reason, if you ever publish the hash results with the data you're putting out publicly, it's probably wise to indicate whether it's the MHash or HASH-MDF implementation of the algorithm. Otherwise, the hash value won't provide much use as a unique identifier for the particular piece of data or for the file.
Some example code to better explain what I mean :
<?php
// Author: holdoffhunger@gmail.com
// SHA-1 Hashing
// ---------------------------------------------------
$mhash_sha1_results = bin2hex(mhash(MHASH_SHA1, "secret"));
$hash_mdf_sha1_results = hash("sha1", "secret", FALSE);
print("MHash SHA-1: $mhash_sha1_results .<br>");
print("HASH-MDF SHA-1: $hash_mdf_sha1_results .<br><br>");
// Whirlpool Hashing
// ---------------------------------------------------
$mhash_whirlpool_results = bin2hex(mhash(MHASH_WHIRLPOOL, "secret"));
$hash_mdf_whirlpool_results = hash("whirlpool", "secret", FALSE);
print("MHash Whirlpool: $mhash_whirlpool_results .<br>");
print("HASH-MDF Whirlpool: $hash_mdf_whirlpool_results .<br><br>");
// CRC32B Hashing
// ---------------------------------------------------
$mhash_crc32b_results = bin2hex(mhash(MHASH_CRC32B, "secret"));
$hash_mdf_crc32b_results = hash("crc32b", "secret", FALSE);
print("MHash CRC32B: $mhash_crc32b_results .<br>");
print("HASH-MDF CRC32B: $hash_mdf_crc32b_results .<br><br>");
// Adler32 Hashing
// ---------------------------------------------------
$mhash_adler32_results = bin2hex(mhash(MHASH_ADLER32, "secret"));
$hash_mdf_adler32_results = hash("adler32", "secret", FALSE);
print("MHash Adler32: $mhash_adler32_results .<br>");
print("HASH-MDF Adler32: $hash_mdf_adler32_results .<br><br>");
?>
Expected Results :
........................
MHash SHA-1: e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4 .
HASH-MDF SHA-1: e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4 .
MHash Whirlpool: e061b87a674ae3880e159ab55ed35d6c5e8a6aefac6ab08304a50588018d377b28639bb15fdeedf006d57e45f7b4298e6dfefceaf7c92c826a708fe6d1156eb3 .
HASH-MDF Whirlpool: e061b87a674ae3880e159ab55ed35d6c5e8a6aefac6ab08304a50588018d377b28639bb15fdeedf006d57e45f7b4298e6dfefceaf7c92c826a708fe6d1156eb3 .
MHash CRC32B: e5e8a25c .
HASH-MDF CRC32B: 5ca2e8e5 .
MHash Adler32: 8702d108 .
HASH-MDF Adler32: 08d10287 .
Official Function Page: http://www.php.net/manual/en/function.mhash.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
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
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
Monday, June 25, 2012
Viewing Hexdec Results as Standard, Non-Scientific Digits in PHP
When given large numbers, the hexdec function automatically converts the value to scientific notation. So, "aa1233123124121241" as a hexadecimal value will be converted to "3.13725790445E+21". If you're converting a hexadecimal value that represents a hash value (md5 or sha), then you need every single bit of that representation to make it useful. By using the number_format function, you can do that perfectly. For example :
<?php
// Author: holdoffhunger@gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_string = "1234567890abcdef1234567890abcdef";
// Converted to Decimal
// ---------------------------------------------
$decimal_result = hexdec($hexadecimal_string);
// Print Pre-Formatted Results
// ---------------------------------------------
print($decimal_result);
// Output Here: "2.41978572002E+37"
// .....................................
// Format Results to View Whole All Digits in Integer
// ---------------------------------------------
// ( Note: All fractional value of the
// Hexadecimal variable are ignored
// in the conversion. )
$current_hashing_algorithm_decimal_result = number_format($decimal_result, 0, '', '');
// Print Formatted Results
// ---------------------------------------------
print($current_hashing_algorithm_decimal_result);
// Output Here: "24197857200151253041252346215207534592"
// .....................................
?>
Official Function Page: http://php.net/manual/en/function.hexdec.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
<?php
// Author: holdoffhunger@gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_string = "1234567890abcdef1234567890abcdef";
// Converted to Decimal
// ---------------------------------------------
$decimal_result = hexdec($hexadecimal_string);
// Print Pre-Formatted Results
// ---------------------------------------------
print($decimal_result);
// Output Here: "2.41978572002E+37"
// .....................................
// Format Results to View Whole All Digits in Integer
// ---------------------------------------------
// ( Note: All fractional value of the
// Hexadecimal variable are ignored
// in the conversion. )
$current_hashing_algorithm_decimal_result = number_format($decimal_result, 0, '', '');
// Print Formatted Results
// ---------------------------------------------
print($current_hashing_algorithm_decimal_result);
// Output Here: "24197857200151253041252346215207534592"
// .....................................
?>
Official Function Page: http://php.net/manual/en/function.hexdec.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
Sunday, June 24, 2012
Convert Hexadecimal to Binary String in PHP
The function 'base_convert' looks like it would be perfect for converting a hexadecimal number to a binary number. That code should look like :
<?php
// Author: holdoffhunger@gmail.com
$hexadecimal_number = "1234567890aBcDeF";
$binary_number = base_convert($hexadecimal_number, 16, 2);
print($binary_number);
?>
However, if the hexadecimal number is too large (more than ~40 characters), the result is always a string of zeros as the binary result. So, I wrote some simple code to specifically handle base conversion with Hexadecimal-to-Binary in PHP :
<?php
// Author: holdoffhunger@gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_number = "1234567890aBcDeF";
// Format Hexadecimal
// ---------------------------------------------
$hexadecimal_to_convert_to_binary_testable = strtolower($hexadecimal_number);
// Get String Length of Hexadecimal
// ---------------------------------------------
$length_of_text_to_convert_to_binary = strlen($hexadecimal_number);
// Initialize Result Variable
// ---------------------------------------------
$results_of_hexadecimal_to_binary_conversion = "";
// Parse Hexadecimal String
// ---------------------------------------------
for($i = 0; $i < $length_of_text_to_convert_to_binary; $i++)
{
$current_char_of_hexadecimal_for_conversion = $hexadecimal_to_convert_to_binary_testable[$i];
switch($current_char_of_hexadecimal_for_conversion)
{
case '0':
$results_of_hexadecimal_to_binary_conversion .= "0000";
break;
case '1':
$results_of_hexadecimal_to_binary_conversion .= "0001";
break;
case '2':
$results_of_hexadecimal_to_binary_conversion .= "0010";
break;
case '3':
$results_of_hexadecimal_to_binary_conversion .= "0011";
break;
case '4':
$results_of_hexadecimal_to_binary_conversion .= "0100";
break;
case '5':
$results_of_hexadecimal_to_binary_conversion .= "0101";
break;
case '6':
$results_of_hexadecimal_to_binary_conversion .= "0110";
break;
case '7':
$results_of_hexadecimal_to_binary_conversion .= "0111";
break;
case '8':
$results_of_hexadecimal_to_binary_conversion .= "1000";
break;
case '9':
$results_of_hexadecimal_to_binary_conversion .= "1001";
break;
case 'a':
$results_of_hexadecimal_to_binary_conversion .= "1010";
break;
case 'b':
$results_of_hexadecimal_to_binary_conversion .= "1011";
break;
case 'c':
$results_of_hexadecimal_to_binary_conversion .= "1100";
break;
case 'd':
$results_of_hexadecimal_to_binary_conversion .= "1101";
break;
case 'e':
$results_of_hexadecimal_to_binary_conversion .= "1110";
break;
case 'f':
$results_of_hexadecimal_to_binary_conversion .= "1111";
break;
}
}
// Print Results
// ---------------------------------------------
print($results_of_hexadecimal_to_binary_conversion);
// Output: (linebreak doesn't appear in results)
// 00010010001101000101011001111000
// 10010000101010111100110111101111
?>
Official Function Page: http://www.php.net/manual/en/function.base-convert.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
<?php
// Author: holdoffhunger@gmail.com
$hexadecimal_number = "1234567890aBcDeF";
$binary_number = base_convert($hexadecimal_number, 16, 2);
print($binary_number);
?>
However, if the hexadecimal number is too large (more than ~40 characters), the result is always a string of zeros as the binary result. So, I wrote some simple code to specifically handle base conversion with Hexadecimal-to-Binary in PHP :
<?php
// Author: holdoffhunger@gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_number = "1234567890aBcDeF";
// Format Hexadecimal
// ---------------------------------------------
$hexadecimal_to_convert_to_binary_testable = strtolower($hexadecimal_number);
// Get String Length of Hexadecimal
// ---------------------------------------------
$length_of_text_to_convert_to_binary = strlen($hexadecimal_number);
// Initialize Result Variable
// ---------------------------------------------
$results_of_hexadecimal_to_binary_conversion = "";
// Parse Hexadecimal String
// ---------------------------------------------
for($i = 0; $i < $length_of_text_to_convert_to_binary; $i++)
{
$current_char_of_hexadecimal_for_conversion = $hexadecimal_to_convert_to_binary_testable[$i];
switch($current_char_of_hexadecimal_for_conversion)
{
case '0':
$results_of_hexadecimal_to_binary_conversion .= "0000";
break;
case '1':
$results_of_hexadecimal_to_binary_conversion .= "0001";
break;
case '2':
$results_of_hexadecimal_to_binary_conversion .= "0010";
break;
case '3':
$results_of_hexadecimal_to_binary_conversion .= "0011";
break;
case '4':
$results_of_hexadecimal_to_binary_conversion .= "0100";
break;
case '5':
$results_of_hexadecimal_to_binary_conversion .= "0101";
break;
case '6':
$results_of_hexadecimal_to_binary_conversion .= "0110";
break;
case '7':
$results_of_hexadecimal_to_binary_conversion .= "0111";
break;
case '8':
$results_of_hexadecimal_to_binary_conversion .= "1000";
break;
case '9':
$results_of_hexadecimal_to_binary_conversion .= "1001";
break;
case 'a':
$results_of_hexadecimal_to_binary_conversion .= "1010";
break;
case 'b':
$results_of_hexadecimal_to_binary_conversion .= "1011";
break;
case 'c':
$results_of_hexadecimal_to_binary_conversion .= "1100";
break;
case 'd':
$results_of_hexadecimal_to_binary_conversion .= "1101";
break;
case 'e':
$results_of_hexadecimal_to_binary_conversion .= "1110";
break;
case 'f':
$results_of_hexadecimal_to_binary_conversion .= "1111";
break;
}
}
// Print Results
// ---------------------------------------------
print($results_of_hexadecimal_to_binary_conversion);
// Output: (linebreak doesn't appear in results)
// 00010010001101000101011001111000
// 10010000101010111100110111101111
?>
Official Function Page: http://www.php.net/manual/en/function.base-convert.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
Subscribe to:
Posts (Atom)