Completed
Push — master ( d732f2...827150 )
by smiley
01:50
created

CryptoBox::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * Class CryptoBox
4
 *
5
 * @filesource   CryptoBox.php
6
 * @created      25.01.2018
7
 * @package      chillerlan\Traits\Crypto
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Traits\Crypto;
14
15
use chillerlan\Traits\{
16
	Container, ContainerInterface
17
};
18
19
/**
20
 * @link https://paragonie.com/book/pecl-libsodium/read/00-intro.md
21
 * @link https://paragonie.com/book/pecl-libsodium/read/01-quick-start.md
22
 */
23
abstract class CryptoBox implements CryptoBoxInterface, ContainerInterface{
24
	use MemzeroDestructorTrait, Container{
25
		__construct as containerConstruct;
26
	}
27
28
	/**
29
	 * @var \chillerlan\Traits\Crypto\CryptoKeyInterface
30
	 */
31
	protected $keypair;
32
33
	/**
34
	 * @var string
35
	 */
36
	protected $box;
37
38
	/**
39
	 * @var string
40
	 */
41
	protected $nonce;
42
43
	/**
44
	 * @var string
45
	 */
46
	protected $message;
47
48
	/** @noinspection PhpMissingParentConstructorInspection */
49
	/**
50
	 * CryptoBox constructor.
51
	 *
52
	 * @param array|null $properties
53
	 *
54
	 * @throws \chillerlan\Traits\Crypto\CryptoException
55
	 */
56
	public function __construct(array $properties = null){
57
58
		if(!extension_loaded('sodium') || !function_exists('sodium_memzero')){
59
			throw new CryptoException('sodium extension (PHP 7.2+) required!'); // @codeCoverageIgnore
60
		}
61
62
		$this->containerConstruct($properties);
0 ignored issues
show
Bug introduced by
The method containerConstruct() does not exist on chillerlan\Traits\Crypto\CryptoBox. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
		$this->/** @scrutinizer ignore-call */ 
63
         containerConstruct($properties);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
	}
64
65
	/**
66
	 * @param int $secretLength
67
	 * @param int $PublicLength
68
	 *
69
	 * @return void
70
	 * @throws \chillerlan\Traits\Crypto\CryptoException
71
	 */
72
	protected function checkKeypair(int $secretLength = null, int $PublicLength = null){
73
74
		if($secretLength !== null){
75
			if(!$this->keypair->secret || strlen($this->keypair->secret) !== $secretLength){
0 ignored issues
show
Bug introduced by
Accessing secret on the interface chillerlan\Traits\Crypto\CryptoKeyInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
76
				throw new CryptoException('invalid secret key');
77
			}
78
		}
79
80
		if($PublicLength !== null){
81
			if(!$this->keypair->public || strlen($this->keypair->public) !== $PublicLength){
0 ignored issues
show
Bug introduced by
Accessing public on the interface chillerlan\Traits\Crypto\CryptoKeyInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
82
				throw new CryptoException('invalid public key');
83
			}
84
		}
85
86
	}
87
88
	protected function checkMessage(string $message):string {
89
		$message = trim($message);
90
91
		if(strlen($message) < 1){
92
			throw new CryptoException('invalid message');
93
		}
94
95
		// @todo: padding?
96
		return $message;
97
	}
98
99
}
100