Completed
Pull Request — master (#3)
by Romain
01:47
created

XHubSignatureHelper::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace Kerox\Messenger\Helper;
3
4
class XHubSignatureHelper
5
{
6
7
    /**
8
     * @param string $content
9
     * @param string $secret
10
     * @param string $signature
11
     * @return bool
12
     */
13
    public static function validate(string $content, string $secret, string $signature): bool
14
    {
15
        list($algorithm, $hash) = explode('=', $signature);
16
17
        return hash_equals(self::compute($algorithm, $content, $secret), $hash);
18
    }
19
20
    /**
21
     * @param string $algorithm
22
     * @param string $content
23
     * @param string $secret
24
     * @return string
25
     */
26
    private function compute(string $algorithm, string $content, string $secret): string
27
    {
28
        return hash_hmac($algorithm, $content, $secret);
29
    }
30
}
31