~43 base64url chars). * @return string Token (>=32 characters for 24+ bytes). */ public static function opaque_token( $bytes = 32 ) { $raw = random_bytes( max( 24, (int) $bytes ) ); // URL-safe Base64 without padding: characters [A-Za-z0-9_-]. return sodium_bin2base64( $raw, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING ); } /** * Constant-time comparison (anti-timing) of two strings. * * @param string $a Reference. * @param string $b Candidate. * @return bool */ public static function compare( $a, $b ) { return is_string( $a ) && is_string( $b ) && hash_equals( $a, $b ); } }