MicroFrameWork/kernel/services/TokenService.php

62 lines
1.2 KiB
PHP
Raw Normal View History

2024-10-22 16:40:40 +03:00
<?php
namespace kernel\services;
use Firebase\JWT\JWT;
2024-10-23 11:38:53 +03:00
use kernel\helpers\Debug;
2024-10-22 16:40:40 +03:00
use Random\RandomException;
class TokenService
{
public static function JWT(string|\OpenSSLCertificate|\OpenSSLAsymmetricKey $key, string $alg, array $payload = []): string
{
return JWT::encode(
payload: $payload,
key: $key,
alg: $alg
);
}
/**
* @throws RandomException
*/
public static function random_bytes(int $ln): string
{
2024-10-23 11:38:53 +03:00
return bin2hex(random_bytes($ln));
}
/**
* @throws RandomException
*/
public static function md5(): string
{
2024-12-13 17:29:36 +03:00
return md5(microtime() . self::getSalt(10) . time());
2024-10-23 11:38:53 +03:00
}
/**
* @throws RandomException
*/
public static function crypt(): string
{
2024-12-13 17:29:36 +03:00
return crypt(microtime(), self::getSalt(20));
2024-10-23 11:38:53 +03:00
}
/**
* @throws RandomException
*/
public static function hash(string $alg): string
{
2024-12-13 17:29:36 +03:00
return hash($alg, self::getSalt(10));
2024-10-23 11:38:53 +03:00
}
/**
* @throws RandomException
*/
2024-12-13 17:29:36 +03:00
public static function getSalt(int $length): string
2024-10-23 11:38:53 +03:00
{
2024-12-13 17:29:36 +03:00
return bin2hex(random_bytes($length));
2024-10-22 16:40:40 +03:00
}
}