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

HmacFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
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