Test Setup Failed
Push — master ( b88989...23c882 )
by smiley
09:01
created

CryptoAbstract   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 44
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bin2hex() 0 3 1
A hex2bin() 0 3 1
A getRandomBytes() 0 3 1
A hmac_hash() 0 3 1
A getPadBytes() 0 9 3
1
<?php
2
/**
3
 * Class CryptoAbstract
4
 *
5
 * @filesource   CryptoAbstract.php
6
 * @created      02.04.2016
7
 * @package      chillerlan\Threema\Crypto
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Threema\Crypto;
14
15
/**
16
 * 
17
 */
18
abstract class CryptoAbstract implements CryptoInterface{
19
20
	/**
21
	 * @inheritdoc
22
	 */
23
	public function bin2hex(string $bin):string{
24
		return bin2hex($bin);
25
	}
26
27
	/**
28
	 * @inheritdoc
29
	 */
30
	public function hex2bin(string $hex):string{
31
		return hex2bin($hex);
32
	}
33
34
	/**
35
	 * @inheritdoc
36
	 */
37
	public function getRandomBytes(int $length):string{
38
		return random_bytes($length);
39
	}
40
41
	/**
42
	 * @inheritdoc
43
	 */
44
	public function hmac_hash(string $str, string $hmacKey):string{
45
		return hash_hmac('sha256', $str, $hmacKey);
46
	}
47
48
	/**
49
	 * @inheritdoc
50
	 */
51
	public function getPadBytes():string {
52
		$padbytes = 0;
53
		
54
		while($padbytes < 1 || $padbytes > 255){
55
			$padbytes = ord($this->getRandomBytes(1));
56
		}
57
58
		return str_repeat(chr($padbytes), $padbytes);
59
	}
60
61
}
62