30 lines
818 B
PHP
30 lines
818 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\pharmacy;
|
|
|
|
final class EjPharmacySignature
|
|
{
|
|
public static function canonical(string $method, string $path, string $timestamp, string $nonce, string $body): string
|
|
{
|
|
return implode("\n", [
|
|
strtoupper(trim($method)),
|
|
$path,
|
|
trim($timestamp),
|
|
trim($nonce),
|
|
hash('sha256', $body),
|
|
]);
|
|
}
|
|
|
|
public static function sign(string $secret, string $canonical): string
|
|
{
|
|
return hash_hmac('sha256', $canonical, $secret);
|
|
}
|
|
|
|
public static function verify(string $secret, string $canonical, string $signature): bool
|
|
{
|
|
return $secret !== '' && $signature !== '' && hash_equals(self::sign($secret, $canonical), strtolower(trim($signature)));
|
|
}
|
|
}
|