Passed
Push — master ( b8e26c...2f113f )
by Remco
05:42
created

Security::get_signature()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Security
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
/**
14
 * Security
15
 *
16
 * @author  Remco Tolsma
17
 * @version 2.0.2
18
 * @since   1.0.0
19
 */
20
class Security {
21
	/**
22
	 * Calculdate signature for specific data.
23
	 *
24
	 * @param Signable $signable    Signable object.
25
	 * @param string   $signing_key Signing Key.
26
	 * @return string|null
27
	 */
28
	public static function get_signature( Signable $signable, $signing_key ) {
29
		$data = $signable->get_signature_data();
30
31
		if ( empty( $data ) ) {
32
			return;
33
		}
34
35
		if ( empty( $signing_key ) ) {
36
			return;
37
		}
38
39
		$signature = hash_hmac(
40
			'sha512',
41
			implode( ',', $data ),
42
			base64_decode( $signing_key )
43
		);
44
45
		return $signature;
46
	}
47
48
	/**
49
	 * Validate signature.
50
	 *
51
	 * @param string $signature_a Signature A.
52
	 * @param string $signature_b Signature B.
53
	 * @return bool True if valid, false otherwise.
54
	 */
55
	public static function validate_signature( $signature_a, $signature_b ) {
56
		if ( empty( $signature_a ) || empty( $signature_b ) ) {
57
			// Empty signature string or null from calculation.
58
			return false;
59
		}
60
61
		return ( 0 === strcasecmp( $signature_a, $signature_b ) );
62
	}
63
}
64