Files
hrc-key-encryptor/HRCEncryptor.php
ansema-service 6a28a8f933 Fixed format key
2023-02-18 00:46:56 +03:00

68 lines
1.5 KiB
PHP

<?php
/**
* Class HRCEncryptor
*
* For key need use Encryption::format($company->code)
*/
class HRCEncryptor
{
const IV = 'AiFFzylt4NjaOflK';
public static function format($key)
{
$reverse = 0;
$n = $key;
while ($n > 0) {
$reverse = $reverse * 10;
$reverse = $reverse + $n % 10;
$n = (int)($n / 10);
}
return pow(($key + $reverse), 2);
}
public static function key($key)
{
$key = self::format($key);
while (strlen($key) < 32) {
$key .= $key;
}
return substr($key, 0, 32);
}
public static function encrypt($content, $key)
{
return base64_encode(openssl_encrypt($content, 'aes-256-cbc', self::key($key), OPENSSL_RAW_DATA, self::IV));
}
public static function decrypt($content, $key)
{
return openssl_decrypt(base64_decode($content), 'aes-256-cbc', self::key($key), OPENSSL_RAW_DATA, self::IV);
}
public static function sign($content, $key)
{
return md5($key . md5($content));
}
public static function check($signature, $public_key)
{
$fields = $_REQUEST;
ksort($fields);
$params = [];
foreach ($fields as $key => $value) {
if ($key != 'signature') {
$params[] = $key . '=' . $value;
}
}
$check = md5($public_key . md5(implode('&', $params)));
return $check == $signature;
}
}