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

CryptoAbstract::bin2hex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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