General Hashing

crc32($s) returns an integer which is the cyclic-redundancy-checksum polynomial of $s. It is 32 bits long. crypt($s1[,$s2]) returns a hashed string of $s1, using the standard Unix DES-based algorithm. $s2 is the salt string to base the hashing on. md5 ($s[,$b=false]) returns the MD5 hash of $s, which is a string storing a 32-character hexadecimal number. If $b is true, the hash is in raw binary format with a length of 16. md5_file($s [,$b=false]) is similar to md5() except that instead of a string, $s is the name of the file to be hashed. sha1($s [,$b=false]) returns the sha1 hash of $s, which is a string storing a 40-character hexadecimal number. If $b is true, the hash is in raw binary format with a length of 20. sha1_file ($s[,b]) is similar to sha1() except that instead of a string, $s is the name of the file to be hashed.

<!DOCTYPE html><html><head></head>
<body><?php

$s = "hello world";
echo crc32($s)."<br />";
echo crypt($s,'ab')."<br />"; // salt fixed here for a reproducible example
echo crypt($s,"ABC")."<br />";
echo md5($s)."<br />";
echo sha1($s)."<br />";

?></body></html>

222957957
abM.kUMZnioHA
AB5Fm/mCd2IgM
5eb63bbbe01eeed093cb22bb8f5acdc3
2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
str_rot13($s) returns a copy of $s with ROT13 performed. ROT13 encoding shifts every letter by 13 places in the alphabet. Passing an encoded string as an argument will return the original version. bin2hex($s) returns a string containing the hexadecimal representation of $s. hex2bin($s) is the reverse of bin2hex(). convert_uuencode($s) returns a string that is $s encoded using the uuencode algorithm. Uuencode translates all strings into printable characters, making them safe for network transmissions. Uuencoded data is about 35% larger than the original. convert_uudecode($s) is the reverse of convert_uuencode(). quoted_ printable_encode($s) returns a quoted printable string created according to RFC2045, Section 6.7, from a 8 bit string. quoted_printable_decode($s)  is the reverse of quoted_printable_encode().

<!DOCTYPE html><html><head></head>
<body><?php

$s = "PHP 5.4.0\n\r";
echo str_rot13($s)."<br />";
echo str_rot13(str_rot13($s))."<br />";
echo bin2hex($s)."<br />";
echo hex2bin(bin2hex($s))."<br />";
echo convert_uuencode($s)."<br />";
echo convert_uudecode(convert_uuencode($s))."<br />";
echo quoted_printable_encode($s)."<br />";
echo quoted_printable_decode( quoted_printable_encode($s))."<br />";

?></body></html>

CUC 5.4.0
PHP 5.4.0
50485020352e342e300a0d
PHP 5.4.0
+4$A0(#4N-"XP"@T` `
PHP 5.4.0
PHP 5.4.0=0A=0D
PHP 5.4.0
hash_algos() returns an array containing the names of all registered hashing algorithms.

<?php
print_r(hash_algos());
?>

Array ( [0] => md2 [1] => md4 [2] => md5 [3] => sha1 [4] => sha224 [5] => sha256 [6] => sha384 [7] => sha512 [8] => ripemd128 [9] => ripemd160 [10] => ripemd256 [11] => ripemd320 [12] => whirlpool [13] => tiger128,3 [14] => tiger160,3 [15] => tiger192,3 [16] => tiger128,4 [17] => tiger160,4 [18] => tiger192,4 [19] => snefru [20] => snefru256 [21] => gost [22] => adler32 [23] => crc32 [24] => crc32b [25] => fnv132 [26] => fnv164 [27] => joaat [28] => haval128,3 [29] => haval160,3 [30] => haval192,3 [31] => haval224,3 [32] => haval256,3 [33] => haval128,4 [34] => haval160,4 [35] => haval192,4 [36] => haval224,4 [37] => haval256,4 [38] => haval128,5 [39] => haval160,5 [40] => haval192,5 [41] => haval224,5 [42] => haval256,5 )
hash_file($s1, $s2 [,$b=false]) returns a hash for string $s2 using algorithm $s1. hash_file($s1, $s2 [,$b=false]) returns a hash for file $s2 using algorithm $s1. If $b is true, raw binary data is output. hash_algos() returns an array containing the names of all registered hashing algorithms.

<?php
echo hash('sha1','Please code PHP well.')."<br/>";
file_put_contents('eg.txt', 'Please code PHP well.');
echo hash_file('sha1', 'eg.txt');
?>

8de35e94d666d764056f4750591c7d00aa416a7e
8de35e94d666d764056f4750591c7d00aa416a7e
hash_init($s1 [, $i=0 [, $s2=NULL]]) initializes an incremental hashing context, using algorithm $s1. $i can be HASH_HMAC. If $i is HASH_HMAC, $s2 specifies the secret key. hash_update($r, $s) pumps string $s into active hashing context $r. hash_update_file($r, $s) pumps data from file $s into active hashing context $r. hash_final($r, $b=false) finalizes an incremental hash (context $r) and returns the resulting digest. If $b is true, raw binary data is output.

<?php
$c = hash_init('md5');
hash_update($c, 'PHP is really fun.');
hash_update($c, 'Do you agree?');
hash_update_file($c,'eg.txt');
echo hash_final($c);
?>

751644bb4b3f66972833c561a2808faf
hash_equals($s1,$s2) compares two strings, $s1 and $s2, in a length-constant amount of time, returning true only if they are identical. Since PHP 5.6, this is the recommended way to compare a user-supplied value (eg. a CSRF token, an API signature, or a password hash) against a known value: an ordinary == comparison exits as soon as it finds a mismatched character, and an attacker who can measure how long the comparison takes could exploit that timing difference to guess the correct value one character at a time. hash_equals() always takes the same amount of time regardless of how many leading characters match.

<?php
$expected  = crypt('12345', '$2a$07$usesomesillystringforsalt$');
$correct   = crypt('12345', '$2a$07$usesomesillystringforsalt$');
$incorrect = crypt('1234', '$2a$07$usesomesillystringforsalt$');

var_dump(hash_equals($expected, $correct));
var_dump(hash_equals($expected, $incorrect));
?>

bool(true) bool(false)