Installation:
composer require ircmaxell/security-lib
Add to composer.json under require if not using Composer globally.
First Use Case:
Use the BinarySafeString class for secure string length comparisons (e.g., password validation, input sanitization):
use Ircmaxell\SecurityLib\BinarySafeString;
$str = "test";
$length = BinarySafeString::length($str); // Returns binary-safe length
Key Classes:
BinarySafeString: For accurate string length calculations (critical for multibyte characters).RandomLib: For cryptographically secure random number generation (if extended in future projects).Where to Look First:
BinarySafeString for immediate utility.Binary-Safe String Handling:
strlen() with BinarySafeString::length() for:
if (BinarySafeString::length($password) < 8)).use Ircmaxell\SecurityLib\BinarySafeString;
$input = "café";
$safeLength = BinarySafeString::length($input); // Returns 4 (not 5)
Integration with Laravel:
use Illuminate\Validation\Rule;
use Ircmaxell\SecurityLib\BinarySafeString;
public function rules()
{
return [
'username' => [
'string',
Rule::function(function ($attribute, $value) {
return BinarySafeString::length($value) <= 32;
}),
],
];
}
namespace App\Http\Middleware;
use Closure;
use Ircmaxell\SecurityLib\BinarySafeString;
class SanitizeInput
{
public function handle($request, Closure $next)
{
$request->merge([
'trimmed_input' => trim($request->input('user_input'), "\x00..\x1F")
]);
return $next($request);
}
}
Testing:
BinarySafeString in unit tests to verify length calculations:
$this->assertEquals(4, BinarySafeString::length("café"));
namespace App\Rules;
use Ircmaxell\SecurityLib\BinarySafeString;
use Illuminate\Contracts\Validation\Rule;
class BinaryLength extends Rule
{
protected $max;
public function __construct($max)
{
$this->max = $max;
}
public function passes($attribute, $value)
{
return BinarySafeString::length($value) <= $this->max;
}
}
Usage:
'username' => ['app:binary-length', 32],
Deprecation Risk:
Limited Features:
BinarySafeString is fully functional. RandomLib is a placeholder (no implementation).Str::random() or random_bytes() instead.Edge Cases in BinarySafeString:
if (!is_string($input)) {
throw new \InvalidArgumentException('Input must be a string');
}
Verify Length Calculations:
BinarySafeString::length() with mb_strlen() for multibyte strings:
$str = "café";
var_dump(BinarySafeString::length($str)); // int(4)
var_dump(mb_strlen($str, 'UTF-8')); // int(4)
UTF-8 vs. ISO-8859-1).Performance:
BinarySafeString::length() is slower than strlen() for ASCII strings. Benchmark in critical paths.Add to Laravel Service Provider:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Facade;
Facade::register('BinarySafeString', function () {
return new \Ircmaxell\SecurityLib\BinarySafeString();
});
$length = \BinarySafeString::length($str);
Custom Binary-Safe Functions:
namespace App\Extensions;
use Ircmaxell\SecurityLib\BinarySafeString as Base;
class BinarySafeString extends Base
{
public static function substring($string, $start, $length)
{
return substr($string, $start, $length);
// Note: Substring may still be unsafe for multibyte. Use with caution.
}
}
Fallback for Missing Features:
RandomLib if needed (e.g., for CSRF tokens):
use RandomLib\Factory;
$factory = new Factory();
$generator = $factory->getMediumStrengthGenerator();
$token = $generator->generateString(32);
How can I help you explore Laravel packages today?