How to use of hash functions in PHP

Created
Modified

MD5 Function

The md5() calculates the md5 hash of a string. Returns the hash as a 32-character hexadecimal number.

$str = "Google";

// 8b36e9207c24c76e6719268e49201d94
echo md5($str);

// bool(true)
md5('240610708') == md5('QNKCDZO')

The comparison is true because both md5() hashes start '0e' so PHP type juggling understands these strings to be scientific notation.

Hash Functions

The hash() function returns a hash value for the given data based on the algorithm like (md5, sha256).

hash(
    string $algo,
    string $data,
    bool $binary = false,
    array $options = []
): string

$str = "Google";

// md5:
$r = hash("md5", $str); 
printf("%-7s %s\n", "md5", $r);

// sha256
$r = hash("sha256", $str);
printf("%-7s %s\n", "sha256", $r);

// crc32
$r = hash("crc32", $str);
printf("%-7s %s\n", "crc32", $r);
md5     8b36e9207c24c76e6719268e49201d94
sha256  ce770667e5f9b0d8f55367bb79419689d90c48451bb33f079f3a9a72ae132de8
crc32   a98f3368

hash_hmac Function

The hash_hmac() function generate a keyed hash value using the HMAC method.

hash_hmac(
    string $algo,
    string $data,
    string $key,
    bool $binary = false
): string

$str = "Google";

// md5
$r = hash("md5", $str); 
printf("%-5s %s\n", "md5", $r);

// md5
$key = "apple";
$r = hash_hmac("md5", $str, $key); 
printf("%-5s %s\n", "hmac", $r);
md5   8b36e9207c24c76e6719268e49201d94
hmac  c7ed9855f3b7ac31b0ee6ae204b3d1c0

Length of the output

How long is hash function? The length of the output or hash depends on the hashing algorithm you use.

$str = "Google";

hash("md5", $str); // md5
hash("sha256", $str); // sha256
hash("crc32", $str); // crc32

foreach (hash_algos() as $v) {

  $r = hash($v, $str);
  printf("%-12s %3d %s\n", $v, strlen($r), $r);
}
md2           32 ef4cce74dab86220df3d2cde2bf7872d
md4           32 26cf807f0af0f1f3da3dd1b730367957
md5           32 8b36e9207c24c76e6719268e49201d94
sha1          40 2b681c0a24baff8899d7163cc7f805c75e1f44e4
sha224        56 b84e52794c142b49fe92323cc27a68fc89b67ee05f6941120e8ac020
sha256        64 ce770667e5f9b0d8f55367bb79419689d90c48451bb33f079f3a9a72ae132de8
sha384        96 2af172307e1317b9c04187cc7f664e5ea7907df8523c409bc2f226ab05f3ca031a966d86db52bc3a3600bd97bd8f2e50
sha512/224    56 291bb8a532905473148259bfe02161e02dc6e05a3cacd6feeaf985f1
sha512/256    64 13470697b72753e0eab9325818943321c7f16bf9ac76b76731e36f09ffd4f079
sha512       128 973807e34fb710b43bafbe55ffcbc7ba91235ee1388e816cb91439d895bd28815734c9886e6ec68262ed8b39f93850e628ba637866726d1e7b726c5e090b0299
sha3-224      56 1256deb617266d03dbab7d4cc5691577e0d771232c998bcab01d9466
sha3-256      64 f86f0d52d7d52195cadd91f21c50c9d756f291798352132de3ed94e098279611
sha3-384      96 68c57cd259865ee3984afc4f00af1a40530bf1ca2ba1aafe7adbf912cd6bbef2755d6d1b936b4fc431b7e6ee6531a956
sha3-512     128 3ad0f436f4de7e5f33a2a4d3707514ec6a33201883959493110c9a9eb5d1dbb6386b58978004d257a446f9cd8fe3439c469af6cc3105ec7d30e3724881e830b7
ripemd128     32 c9dd129bb987a91e85c94abd31558735
ripemd160     40 a986a86f6241845eaa3c9875a58e123b67933cfe
ripemd256     64 0305fc62ad0eed097e19ffb71c8274c49ef6766fa35785dc3c1a5357ff40abe8
ripemd320     80 43b67aed55307a827b92423ed8a6cc2316a8b976c075ed08d8ed9bdbbf56ab2c2a62f9f4625818ff
whirlpool    128 6cff68ca32b1eccc5d8e4da6a5d0c2ebd7e7f27da9883744e6ecbcbfec246ccab86db69690795a454a2ca59ef862bf355447d1a7d90df7de78efd2d4980db0de
tiger128,3    32 90a8c6cd64b63e2b69ca530ce72a12ac
tiger160,3    40 90a8c6cd64b63e2b69ca530ce72a12ac5e951c8f
tiger192,3    48 90a8c6cd64b63e2b69ca530ce72a12ac5e951c8f1c66e76d
tiger128,4    32 4bc0eaf32a5d23c9b9471001d50011d1
tiger160,4    40 4bc0eaf32a5d23c9b9471001d50011d13c4b7041
tiger192,4    48 4bc0eaf32a5d23c9b9471001d50011d13c4b7041dc26d6bd
snefru        64 6cd79d0616aedb2c77bda3cb086de5ba2df50ab0914d89de28147424a6c29deb
snefru256     64 6cd79d0616aedb2c77bda3cb086de5ba2df50ab0914d89de28147424a6c29deb
gost          64 c746a4916b524a8ee3c2d17e422604e958a3f3b0a1af5e9b48fdffc17289dbe8
gost-crypto   64 e99bcc7cd375ba552cdfa4738b02686d166f7516b12c9a34f2ca7f793ab2c448
adler32        8 0809025e
crc32          8 a98f3368
crc32b         8 62b0f067
fnv132         8 ef7f4f70
fnv1a32        8 e3b85fa6
fnv164        16 c9b5f41dc6c6b290
fnv1a64       16 d517473001a04866
joaat          8 30359b9f
haval128,3    32 6db80f18a37f1c7376c12b096d08a8cf
haval160,3    40 a0c42322bc198714b776d345c454f9281b01a666
haval192,3    48 7d15ecf012ccab23a237aa8a8b9c9a53ec9f7c34335cf1cf
haval224,3    56 db9e76463ef171e0a40aab1a0bbaaf40fcb9374b52cf250427facc5f
haval256,3    64 6c3b9c7f01eebb13a04d500fddfa6bb561ca70083bbd957960a1faa4ee3c027d
haval128,4    32 89b80917072d844deb7e73ce8d1210f1
haval160,4    40 0a6f00ae5da588cf85829798965f8c443e7aa611
haval192,4    48 e786ed6fe5c7b145e45e1f0ffea8a3d74b2747f9f4f2b583
haval224,4    56 c9610a8024d01588a4e134a38b27da49545bdb53d52caf6c7f5cc4a8
haval256,4    64 2658e0d1d84c2148206e4b3e50fcc0748878f07599475d6acf98c00ff62304f5
haval128,5    32 0c00b8283c4b5be93ed282cc9dd58ec1
haval160,5    40 71a0ccc422d64c00b6fbac61c32722c62dadde54
haval192,5    48 fb47a69eca151a9678fdce96a003e32a58dc0d2e2a135b1a
haval224,5    56 168fa42b4d0b1bd9b0602b337aefa93ff418b47cfe8be73e10b6145d
haval256,5    64 87cde1d87a5fe43caa8e060b1d0aca628dc7280b648c9b234217eb5ad095fafc

Related Tags

#hash# #md5# #sha256# #crc32# #hmac#