From 4d92459731584b0ba76a978a8fbe868013e9f147 Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 11 Jul 2023 22:00:24 +0000 Subject: [PATCH] Removed hash function wrappers. --- VERSION | 2 +- src/Security/HashAlgorithm.php | 67 ---------------- src/Security/HashAlgorithmInfo.php | 120 ----------------------------- 3 files changed, 1 insertion(+), 188 deletions(-) delete mode 100644 src/Security/HashAlgorithm.php delete mode 100644 src/Security/HashAlgorithmInfo.php diff --git a/VERSION b/VERSION index bdd2a71..f5eb630 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2307.112158 +0.2307.112200 diff --git a/src/Security/HashAlgorithm.php b/src/Security/HashAlgorithm.php deleted file mode 100644 index a73ace1..0000000 --- a/src/Security/HashAlgorithm.php +++ /dev/null @@ -1,67 +0,0 @@ -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); - } -} diff --git a/src/Security/HashAlgorithmInfo.php b/src/Security/HashAlgorithmInfo.php deleted file mode 100644 index b5588c1..0000000 --- a/src/Security/HashAlgorithmInfo.php +++ /dev/null @@ -1,120 +0,0 @@ -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();