<?php
abstract class Hash
{
protected $algo;
private $bytes;
protected $data;
public function __construct(string $data)
{
$this->bytes = strlen(hash($this->algo, '', true));
$this->data = $data;
}
public function getRaw() : string
{
return $this->data;
}
public function getHex() : string
{
return bin2hex($this->data);
}
public function getBase64() : string
{
return base64_encode($this->data);
}
public static function fromHex(string $data) : Hash
{
return new static(hex2bin($data));
}
public static function fromBase64(string $data) : Hash
{
return new static(base64_decode($data));
}
public static function getClass(string $algo) : string
{
$prefix = ucfirst(strtolower($algo));
$class = implode('\\', [__NAMESPACE__, "${prefix}Hash"]);
return $class;
}
}
final class Md5Hash extends Hash { protected $algo = 'md5'; }
final class Sha256Hash extends Hash { protected $algo = 'sha256'; }
final class Sha512Hash extends Hash { protected $algo = 'sha512'; }
foreach(explode('|', 'md5|sha256|sha512') as $a){
$submitted[$a] = hash($a, 'foobar');
}
foreach($submitted as $algo => $hexVal){
$hashes[] = (Hash::getClass($algo))::fromHex($hexVal);
}
var_dump($hashes);
- Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/bSBD0 on line 44
array(3) {
[0]=>
object(Md5Hash)#1 (3) {
["algo":protected]=>
string(3) "md5"
["bytes":"Hash":private]=>
int(16)
["data":protected]=>
string(16) "8X�"0�<�_0fC�?"
}
[1]=>
object(Sha256Hash)#2 (3) {
["algo":protected]=>
string(6) "sha256"
["bytes":"Hash":private]=>
int(32)
["data":protected]=>
string(32) "ë��7 譐G�9Fk<�t��8=J9`qL����"
}
[2]=>
object(Sha512Hash)#3 (3) {
["algo":protected]=>
string(6) "sha512"
["bytes":"Hash":private]=>
int(64)
["data":protected]=>
string(64) "
P&�9�+�&�g<U��4-R2�=3�aj�i�X|�c_i%�l60��'5 �>�$%"
}
}
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
- array(3) {
[0]=>
object(Md5Hash)#1 (3) {
["algo":protected]=>
string(3) "md5"
["bytes":"Hash":private]=>
int(16)
["data":protected]=>
string(16) "8X�"0�<�_0fC�?"
}
[1]=>
object(Sha256Hash)#2 (3) {
["algo":protected]=>
string(6) "sha256"
["bytes":"Hash":private]=>
int(32)
["data":protected]=>
string(32) "ë��7 譐G�9Fk<�t��8=J9`qL����"
}
[2]=>
object(Sha512Hash)#3 (3) {
["algo":protected]=>
string(6) "sha512"
["bytes":"Hash":private]=>
int(64)
["data":protected]=>
string(64) "
P&�9�+�&�g<U��4-R2�=3�aj�i�X|�c_i%�l60��'5 �>�$%"
}
}
- Output for 5.6.0 - 5.6.40
- Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/bSBD0 on line 16
Process exited with code 255.
preferences:
199.94 ms | 401 KiB | 287 Q