57 lines
2.3 KiB
PHP
57 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace web\admin\demo\dyn;
|
|
|
|
class rsa {
|
|
public static function json_checkrsa() {
|
|
$post = new \ciy\post();
|
|
$pubkey = $post->get('pubkey');
|
|
if (empty($pubkey))
|
|
return errjson('请填写公钥');
|
|
$sign = $post->get('sign');
|
|
if (empty($sign))
|
|
return errjson('请填写签名');
|
|
$hash = $post->get('hash');
|
|
if (empty($hash))
|
|
return errjson('请填写hash');
|
|
$signbin = hex2bin($sign);
|
|
if ($signbin === false)
|
|
return errjson('签名格式错误');
|
|
$hashbin = hex2bin($hash);
|
|
if ($hashbin === false)
|
|
return errjson('hash格式错误');
|
|
$signKey = $pubkey;
|
|
if (strpos($signKey, '-----BEGIN RSA PUBLIC KEY-----') === false && strpos($signKey, '-----BEGIN PUBLIC KEY-----') === false)
|
|
$signKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($signKey, 64, "\n") . "\n-----END PUBLIC KEY-----";
|
|
$result = openssl_verify($hashbin, $signbin, $signKey, OPENSSL_ALGO_SHA256);
|
|
if ($result === 0)
|
|
$ret['msg'] = '验签失败';
|
|
else if ($result === 1)
|
|
$ret['msg'] = '验签成功';
|
|
else
|
|
return errjson('验签错误:' . openssl_error_string());
|
|
return succjson($ret);
|
|
}
|
|
public static function json_signrsa() {
|
|
$post = new \ciy\post();
|
|
$prikey = $post->get('prikey');
|
|
if (empty($prikey))
|
|
return errjson('请填写私钥');
|
|
$hash = $post->get('hash');
|
|
if (empty($hash))
|
|
return errjson('请填写hash');
|
|
$hashbin = hex2bin($hash);
|
|
if ($hashbin === false)
|
|
return errjson('hash格式错误');
|
|
$privateKey = $prikey;
|
|
if (strpos($privateKey, '-----BEGIN RSA PRIVATE KEY-----') === false && strpos($privateKey, '-----BEGIN PRIVATE KEY-----') === false)
|
|
$privateKey = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($privateKey, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
|
|
$result = openssl_sign($hashbin, $signbin, $privateKey, OPENSSL_ALGO_SHA256);
|
|
if ($result !== true)
|
|
return errjson('签名错误:' . openssl_error_string());
|
|
$ret['sign'] = bin2hex($signbin);
|
|
$ret['from'] = 'php';
|
|
return succjson($ret);
|
|
}
|
|
}
|