<?php

namespace WPProofShield\Security;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


final class PowVerifier {

	public function verify( string $encodedPayload, int $counter, int $difficulty, ?string $providedHash = null ): bool {
		if ( $counter < 0 || $counter > 1000000000 ) {
			return false;
		}

		$hash = hash( 'sha256', $encodedPayload . ':' . $counter );
		if ( $providedHash !== null && $providedHash !== '' && ! hash_equals( $hash, $providedHash ) ) {
			return false;
		}

		return str_starts_with( $hash, str_repeat( '0', max( 1, $difficulty ) ) );
	}
}
