Removed hash function wrappers.

This commit is contained in:
Pachira 2023-07-11 22:00:24 +00:00
parent 828dc867a1
commit 4d92459731
3 changed files with 1 additions and 188 deletions

View file

@ -1 +1 @@
0.2307.112158
0.2307.112200

View file

@ -1,67 +0,0 @@
<?php
// HashAlgorithm.php
// Created: 2021-06-16
// Updated: 2022-02-27
namespace Index\Security;
use HashContext;
use InvalidArgumentException;
use RuntimeException;
use Index\ICloneable;
use Index\IO\GenericStream;
class HashAlgorithm implements ICloneable {
private HashAlgorithmInfo $info;
private HashContext $context;
public function __construct(HashAlgorithmInfo $info, ?HashContext $context = null) {
$this->info = $info;
if($context === null)
$this->reset();
else
$this->context = $context;
}
public function reset(): void {
$this->context = hash_init($this->info->getName(), $this->info->isHMAC() ? HASH_HMAC : 0, $this->info->getKey());
}
public function update(string $data): void {
hash_update($this->context, $data);
}
public function updateStream(mixed $stream, int $length = -1): int {
if($stream instanceof GenericStream)
$stream = $stream->getResource();
if(!is_resource($stream))
throw new InvalidArgumentException('$stream must be a resource or an instance of Index\IO\GenericStream.');
return hash_update_stream($this->context, $stream, $length);
}
public function updateFile(string $fileName, mixed $streamContext = null): void {
if($streamContext !== null && !is_resource($streamContext))
throw new InvalidArgumentException('$streamContext must be null or a resource.');
if(!hash_update_file($this->context, $fileName, $streamContext))
throw new RuntimeException('File hash failed.');
}
public function finalise(bool $hex = false): string {
return hash_final($this->context, !$hex);
}
public function clone(): mixed {
return new HashAlgorithm($this->info, hash_copy($this->context));
}
public static function equals(
HashAlgorithm|string $trusted,
HashAlgorithm|string $foreign
): bool {
if($trusted instanceof HashAlgorithm)
$trusted = $trusted->finalise(false);
if($foreign instanceof HashAlgorithm)
$foreign = $foreign->finalise(false);
return hash_equals($trusted, $foreign);
}
}

View file

@ -1,120 +0,0 @@
<?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();