Files
trafficpumper/app/Services/EarnDeviceIdentity.php
2026-08-29 15:53:53 +03:00

42 lines
1.0 KiB
PHP

<?php
namespace App\Services;
use RuntimeException;
class EarnDeviceIdentity
{
public function normalize(string $deviceId): string
{
return trim($deviceId);
}
public function valid(string $deviceId): bool
{
$length = strlen($deviceId);
return $length >= 4 && $length <= 255 && preg_match('/^[\x20-\x7E]+$/D', $deviceId) === 1;
}
public function hash(string $deviceId): string
{
$key = (string) config('earn.device.hash_key');
if (strlen($key) < 32) {
throw new RuntimeException('Earn device hashing is not configured.');
}
return hash_hmac('sha256', $this->normalize($deviceId), $key);
}
public function preview(string $deviceId): string
{
$normalized = $this->normalize($deviceId);
if (strlen($normalized) <= 10) {
return substr($normalized, 0, 2).'••••'.substr($normalized, -2);
}
return substr($normalized, 0, 6).'…'.substr($normalized, -5);
}
}