CryptoAbstract::bin2hex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
abstract class CryptoAbstract implements CryptoInterface{
16
17
	/**
18
	 * @inheritdoc
19
	 */
20
	public function bin2hex(string $bin):string{
21
		return bin2hex($bin);
22
	}
23
24
	/**
25
	 * @inheritdoc
26
	 */
27
	public function hex2bin(string $hex):string{
28
		return hex2bin($hex);
29
	}
30
31
	/**
32
	 * @inheritdoc
33
	 */
34
	public function getRandomBytes(int $length):string{
35
		return random_bytes($length);
36
	}
37
38
	/**
39
	 * @inheritdoc
40
	 */
41
	public function hmac_hash(string $str, string $hmacKey):string{
42
		return hash_hmac('sha256', $str, $hmacKey);
43
	}
44
45
	/**
46
	 * @inheritdoc
47
	 */
48
	public function getPadBytes():string {
49
		$padbytes = 0;
50
51
		while($padbytes < 1 || $padbytes > 255){
52
			$padbytes = ord($this->getRandomBytes(1));
53
		}
54
55
		return str_repeat(chr($padbytes), $padbytes);
56
	}
57
58
}
59