Completed
Push — master ( ec2dfb...bb6a7d )
by Steve
20:11 queued 09:39
created

HmacFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
ccs 8
cts 8
cp 1
rs 10
wmc 3
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHmac() 0 6 2
1
<?php
2
namespace Elgg\Security;
3
4
use ElggCrypto;
5
use Elgg\Database\SiteSecret;
6
7
/**
8
 * Provides a factory for HMAC objects
9
 */
10
class HmacFactory {
11
12
	/**
13
	 * @var SiteSecret
14
	 */
15
	private $site_secret;
16
17
	/**
18
	 * @var ElggCrypto
19
	 */
20
	private $crypto;
21
22
	/**
23
	 * Constructor
24
	 *
25
	 * @param SiteSecret $secret Site secret
26
	 * @param ElggCrypto $crypto Elgg crypto service
27
	 */
28 10
	public function __construct(SiteSecret $secret, ElggCrypto $crypto) {
29 10
		$this->site_secret = $secret;
30 10
		$this->crypto = $crypto;
31 10
	}
32
33
	/**
34
	 * Get an HMAC token builder/validator object
35
	 *
36
	 * @param mixed  $data HMAC data or serializable data
37
	 * @param string $algo Hash algorithm
38
	 * @param string $key  Optional key (default uses site secret)
39
	 *
40
	 * @return Hmac
41
	 */
42 59
	public function getHmac($data, $algo = 'sha256', $key = '') {
43 59
		if (!$key) {
44 52
			$key = $this->site_secret->get(true);
45
		}
46 59
		return new Hmac($key, [$this->crypto, 'areEqual'], $data, $algo);
47
	}
48
}
49