<?php

namespace SugarCalendarBookings\Support\Helpers;

use Exception;

/**
 * Class Security.
 *
 * Security-related helper functions.
 *
 * @since 1.2.0
 */
class Security {

	/**
	 * Generate a cryptographically secure random string.
	 *
	 * Uses random_bytes() for cryptographically secure random number generation (CSPRNG).
	 * Suitable for security tokens, OAuth state parameters, session IDs, and CSRF tokens.
	 *
	 * @since 1.2.0
	 *
	 * @param int $length Number of random bytes to generate (output will be 2x in hex).
	 *
	 * @return string Hexadecimal string of length $length * 2.
	 */
	public static function generate_secure_random( $length = 32 ) {

		try {
			// Use random_bytes for cryptographically secure random data.
			$random_bytes = random_bytes( $length );

			return bin2hex( $random_bytes );
		} catch ( Exception $e ) {
			// Fallback to wp_generate_password if random_bytes fails.
			return wp_generate_password( $length * 2, true, true );
		}
	}
}
