binary function myhash($alg, $content) { return hash($alg, $content, true); } // Base64 encode: binary -> text function encode($b) { return base64_encode($b); } // hash: text -> Base64 text function hash_encode($alg, $content) { return encode(myhash($alg, $content)); } // test define('TEXT', 'This is some random text to be used to test encryption.'); function test($label, $alg) { echo $label . ":\r\n"; $hashval = hash_encode($alg, TEXT); echo ' ' . $hashval . "\r\n"; $hashval2 = hash_encode($alg, TEXT . TEXT); echo ' ' . $hashval2 . "\r\n"; } function test2() { if(hash_encode('SHA256', TEXT) != 'oJM3t4y26uLCLtiLLNP8bhZuZi24znFEdB+QnM2d0w4=') { echo 'Ooops'; } } test('SHA-2 256 bit', 'SHA256'); test('SHA-2 384 bit', 'SHA384'); test('SHA-2 512 bit', 'SHA512'); //test('SHA-3 256 bit', 'SHA3-256'); // require PHP 7.1+ //test('SHA-3 384 bit', 'SHA3-384'); // require PHP 7.1+ //test('SHA-3 512 bit', 'SHA3-512'); // require PHP 7.1+ test2(); ?>