Failed Conditions
Push — feature/post-pay ( 4d43ec...e3663d )
by Remco
04:52
created

Security::validate_signature()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
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 4
	public static function get_signature( Signable $signable, $signing_key ) {
29 4
		$data = $signable->get_signature_data();
30
31 4
		if ( empty( $data ) ) {
32
			return null;
33
		}
34
35 4
		if ( empty( $signing_key ) ) {
36
			return null;
37
		}
38
39 4
		$decoded_signing_key = base64_decode( $signing_key );
40
41 4
		if ( false === $decoded_signing_key ) {
42
			return null;
43
		}
44
45 4
		$combined = implode( ',', $data );
46
47 4
		var_dump( $combined );
0 ignored issues
show
Security Debugging Code introduced by
var_dump($combined) looks like debug code. Are you sure you do not want to remove it?
Loading history...
48
49 4
		$signature = hash_hmac(
50 4
			'sha512',
51 4
			$combined,
52 4
			$decoded_signing_key
53
		);
54
55 4
		return $signature;
56
	}
57
58
	/**
59
	 * Validate signature.
60
	 *
61
	 * @param string $signature_a Signature A.
62
	 * @param string $signature_b Signature B.
63
	 * @return bool True if valid, false otherwise.
64
	 */
65 3
	public static function validate_signature( $signature_a, $signature_b ) {
66 3
		if ( empty( $signature_a ) || empty( $signature_b ) ) {
67
			// Empty signature string or null from calculation.
68
			return false;
69
		}
70
71 3
		return ( 0 === strcasecmp( $signature_a, $signature_b ) );
72
	}
73
}
74