1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server\DouGong;
-
- class BaseFunc
- {
- static function json($data)
- {
- return json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
- }
-
- static function get_data_json(array $data) : array
- {
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $data[$key] = static::json($value);
- }
- }
-
- return $data;
- }
-
-
-
- static function sha_with_rsa_sign($data, $rsaPrivateKey, int $alg = OPENSSL_ALGO_SHA256): string
- {
- ksort($data);
-
- $data = static::json(static::get_data_json($data));
-
- $key = "-----BEGIN PRIVATE KEY-----\n" .
- wordwrap($rsaPrivateKey, 64, "\n", true) .
- "\n-----END PRIVATE KEY-----";
-
- $signature = '';
-
- openssl_sign($data, $signature, $key, $alg);
-
- return base64_encode($signature);
- }
-
-
-
- static function verifySign_sort(string $signature, string $data, string $rsaPublicKey, int $alg = OPENSSL_ALGO_SHA256)
- {
- $key = "-----BEGIN PUBLIC KEY-----\n" .
- wordwrap($rsaPublicKey, 64, "\n", true) .
- "\n-----END PUBLIC KEY-----";
-
- return openssl_verify($data, base64_decode($signature), $key, $alg);
- }
- }
|