121 lines
3.5 KiB
PHP
121 lines
3.5 KiB
PHP
<?php
|
|
// HashAlgorithmInfo.php
|
|
// Created: 2021-06-16
|
|
// Updated: 2022-02-27
|
|
|
|
namespace Index\Security;
|
|
|
|
use RuntimeException;
|
|
|
|
class HashAlgorithmInfo {
|
|
private string $name;
|
|
private bool $hmac;
|
|
private ?string $key;
|
|
|
|
public function __construct(string $algorithm, ?string $key = null) {
|
|
$this->name = $algorithm;
|
|
$this->hmac = $key === null;
|
|
$this->key = $key;
|
|
}
|
|
|
|
private static bool $constructed = false;
|
|
private static HashAlgorithmInfo $md5;
|
|
private static HashAlgorithmInfo $sha1;
|
|
private static HashAlgorithmInfo $sha2_256;
|
|
private static HashAlgorithmInfo $sha2_512;
|
|
private static HashAlgorithmInfo $sha3_256;
|
|
private static HashAlgorithmInfo $sha3_512;
|
|
private static HashAlgorithmInfo $crc32;
|
|
private static HashAlgorithmInfo $crc32b;
|
|
private static HashAlgorithmInfo $crc32c;
|
|
|
|
public static function construct(): void {
|
|
if(self::$constructed)
|
|
throw new RuntimeException('Static constructor was already called.');
|
|
self::$constructed = true;
|
|
self::$md5 = new HashAlgorithmInfo('md5');
|
|
self::$sha1 = new HashAlgorithmInfo('sha1');
|
|
self::$sha2_256 = new HashAlgorithmInfo('sha256');
|
|
self::$sha2_512 = new HashAlgorithmInfo('sha512');
|
|
self::$sha3_256 = new HashAlgorithmInfo('sha3-256');
|
|
self::$sha3_512 = new HashAlgorithmInfo('sha3-512');
|
|
self::$crc32 = new HashAlgorithmInfo('crc32');
|
|
self::$crc32b = new HashAlgorithmInfo('crc32b');
|
|
self::$crc32c = new HashAlgorithmInfo('crc32c');
|
|
}
|
|
|
|
public function getName(): string {
|
|
return $this->name;
|
|
}
|
|
|
|
public function isHMAC(): bool {
|
|
return $this->hmac;
|
|
}
|
|
|
|
public function getKey(): string {
|
|
return $this->key;
|
|
}
|
|
|
|
public function create(): HashAlgorithm {
|
|
return new HashAlgorithm($this);
|
|
}
|
|
|
|
public static function hashAlgos(): array {
|
|
return hash_algos();
|
|
}
|
|
|
|
public static function hmacAlgos(): array {
|
|
return hash_hmac_algos();
|
|
}
|
|
|
|
public static function md5(?string $key = null): HashAlgorithmInfo {
|
|
if($key === null)
|
|
return new HashAlgorithmInfo('md5', $key);
|
|
return self::$md5;
|
|
}
|
|
|
|
public static function sha1(?string $key = null): HashAlgorithmInfo {
|
|
if($key === null)
|
|
return new HashAlgorithmInfo('sha1', $key);
|
|
return self::$sha1;
|
|
}
|
|
|
|
public static function sha2_256(?string $key = null): HashAlgorithmInfo {
|
|
if($key === null)
|
|
return new HashAlgorithmInfo('sha256', $key);
|
|
return self::$sha2_256;
|
|
}
|
|
|
|
public static function sha2_512(?string $key = null): HashAlgorithmInfo {
|
|
if($key === null)
|
|
return new HashAlgorithmInfo('sha512', $key);
|
|
return self::$sha2_512;
|
|
}
|
|
|
|
public static function sha3_256(?string $key = null): HashAlgorithmInfo {
|
|
if($key === null)
|
|
return new HashAlgorithmInfo('sha3-256', $key);
|
|
return self::$sha3_256;
|
|
}
|
|
|
|
public static function sha3_512(?string $key = null): HashAlgorithmInfo {
|
|
if($key === null)
|
|
return new HashAlgorithmInfo('sha3-512', $key);
|
|
return self::$sha3_512;
|
|
}
|
|
|
|
public static function crc32(): HashAlgorithmInfo {
|
|
return self::$crc32;
|
|
}
|
|
|
|
public static function crc32b(): HashAlgorithmInfo {
|
|
return self::$crc32b;
|
|
}
|
|
|
|
public static function crc32c(): HashAlgorithmInfo {
|
|
return self::$crc32c;
|
|
}
|
|
}
|
|
|
|
HashAlgorithmInfo::construct();
|