You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.1 KiB
PHP
86 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudSecrets;
|
|
|
|
/** Encrypts Telegram credentials with a purpose-separated PHP data key. */
|
|
final class TicketTelegramCredentialCipher
|
|
{
|
|
private const PREFIX = 'sbox:telegram:v1:';
|
|
private const PURPOSES = ['bot_token', 'webhook_secret'];
|
|
private const KEY_PURPOSES = [
|
|
'bot_token' => 'telegram-bot-token',
|
|
'webhook_secret' => 'telegram-webhook-secret',
|
|
];
|
|
|
|
public static function encrypt(string $plainText, string $purpose): string
|
|
{
|
|
self::assertPurpose($purpose);
|
|
if ($plainText === '') {
|
|
return '';
|
|
}
|
|
if (!function_exists('sodium_crypto_secretbox')) {
|
|
throw new \RuntimeException('PHP sodium 扩展未安装,无法加密 Telegram 凭据');
|
|
}
|
|
|
|
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
|
$cipherText = sodium_crypto_secretbox($plainText, $nonce, self::key($purpose));
|
|
return self::PREFIX . base64_encode($nonce . $cipherText);
|
|
}
|
|
|
|
public static function decrypt(string $encoded, string $purpose): string
|
|
{
|
|
self::assertPurpose($purpose);
|
|
$encoded = trim($encoded);
|
|
if ($encoded === '') {
|
|
return '';
|
|
}
|
|
if (!str_starts_with($encoded, self::PREFIX)) {
|
|
throw new \RuntimeException('Telegram 凭据不是受支持的加密格式,请在后台重新保存');
|
|
}
|
|
if (!function_exists('sodium_crypto_secretbox_open')) {
|
|
throw new \RuntimeException('PHP sodium 扩展未安装,无法解密 Telegram 凭据');
|
|
}
|
|
|
|
$payload = base64_decode(substr($encoded, strlen(self::PREFIX)), true);
|
|
if ($payload === false || strlen($payload) <= SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) {
|
|
throw new \RuntimeException('Telegram 凭据密文格式无效,请在后台重新保存');
|
|
}
|
|
$plainText = sodium_crypto_secretbox_open(
|
|
substr($payload, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES),
|
|
substr($payload, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES),
|
|
self::key($purpose)
|
|
);
|
|
if ($plainText === false) {
|
|
throw new \RuntimeException('Telegram 凭据解密失败,请检查 cloud_data_key');
|
|
}
|
|
return $plainText;
|
|
}
|
|
|
|
public static function isCiphertext(string $value): bool
|
|
{
|
|
return $value === '' || str_starts_with($value, self::PREFIX);
|
|
}
|
|
|
|
private static function key(string $purpose): string
|
|
{
|
|
try {
|
|
$master = CloudSecrets::key(self::KEY_PURPOSES[$purpose]);
|
|
} catch (CloudError $exception) {
|
|
throw new \RuntimeException(
|
|
'Telegram 凭据加密密钥不可用,请先配置有效的 cloud_data_key 或 cloud_data_key_file'
|
|
);
|
|
}
|
|
return hash_hkdf('sha256', $master, SODIUM_CRYPTO_SECRETBOX_KEYBYTES, 'virt-cloud:telegram:' . $purpose);
|
|
}
|
|
|
|
private static function assertPurpose(string $purpose): void
|
|
{
|
|
if (!in_array($purpose, self::PURPOSES, true)) {
|
|
throw new \InvalidArgumentException('不支持的 Telegram 凭据类型');
|
|
}
|
|
}
|
|
}
|