Passed
Push — master ( 1dab8b...f743f0 )
by Dmitry
02:07
created

Sign::get_pub_key()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EasyPay;
4
5
use EasyPay\Log as Log;
6
use EasyPay\Exception;
7
use EasyPay\Key as Key;
8
use EasyPay\OpenSSL as OpenSSL;
9
10
class Sign
11
{
12
    public function __construct() {}
13
14
    /**
15
     *      Verify signature of request
16
     *
17
     *      @param array $options
18
     */
19
    public function verify($request_str, $sign, $options)
20
    {
21
        if (isset($options['UseSign']) && ($options['UseSign'] === true))
22
        {
23
            $this->check_verify_sign_result(
24
                $result = (new OpenSSL())->verify(
25
                    str_replace($sign, '', $request_str),
26
                    pack("H*", $sign),
27
                    (new OpenSSL())->get_pub_key($this->get_pub_key($options))
28
                )
29
            );
30
        }
31
    }
32
33
    /**
34
     *      load file with easysoft public key
35
     *
36
     *      @param array $options
37
     *      @throws Exception\Runtime
38
     *      @return string
39
     */
40
    protected function get_pub_key($options)
41
    {
42
        if ( ! isset($options['EasySoftPKey']))
43
        {
44
            throw new Exception\Runtime('The parameter EasySoftPKey is not set!', -94);
45
        }
46
47
        return (new Key())->get($options['EasySoftPKey'], 'public');
48
    }
49
50
    /**
51
     *      check result of openssl verify signature
52
     *
53
     *      @param integer $result
54
     *      @throws Exception\Sign
55
     */
56
    protected function check_verify_sign_result($result)
57
    {
58
        if ($result == -1)
59
        {
60
            throw new Exception\Sign('Error verify signature of request!', -96);
61
        }
62
        elseif ($result == 0)
63
        {
64
            throw new Exception\Sign('Signature of request is incorrect!', -95);
65
        }
66
    }
67
}
68