Test Failed
Push — master ( ebd18c...f86896 )
by Dani
17:51
created

Signature   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 33
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A verify() 0 15 5
1
<?php
2
3
namespace Postpay\Http;
4
5
class Signature
6
{
7
    /**
8
     * @const int Default tolarence in seconds.
9
     */
10
    const DEFAULT_TOLERANCE = 300;
11
12
    /**
13
     * Verifies the signature header.
14
     *
15
     * @param string $payload
16
     * @param string $header
17
     * @param string $secret
18
     * @param int    $tolerance
19
     *
20
     * @return bool
21
     */
22 3
    public static function verify(
23
        $payload,
24
        $header,
25
        $secret,
26
        $tolerance = self::DEFAULT_TOLERANCE
27
    ) {
28 3
        list($timestamp, $signature) = explode(':', trim($header), 2);
29
30 3
        if (!is_numeric($timestamp) || empty($signature) ||
31 3
                (($tolerance > 0) && (abs(time() - $timestamp) > $tolerance))) {
32 2
            return false;
33
        }
34 1
        $expected = hash_hmac('sha256', "{$timestamp}:{$payload}", $secret);
35 1
        return hash_equals($expected, $signature);
36
    }
37
}
38