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

SealedBox   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 14 2
A create() 0 8 1
1
<?php
2
/**
3
 * Class SealedBox
4
 *
5
 * @filesource   SealedBox.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
class SealedBox extends CryptoBox{
16
17
	public function create(string $message):CryptoBoxInterface{
18
		$this->checkKeypair(null, SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
19
20
		$this->box = sodium_crypto_box_seal($this->checkMessage($message), $this->keypair->public);
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...
21
22
		sodium_memzero($message);
23
24
		return $this;
25
	}
26
27
	public function open(string $box_bin):CryptoBoxInterface{
28
		$this->checkKeypair(SODIUM_CRYPTO_BOX_SECRETKEYBYTES, SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
29
30
		$keypair       = sodium_crypto_box_keypair_from_secretkey_and_publickey($this->keypair->secret, $this->keypair->public);
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...
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...
31
		$this->message = sodium_crypto_box_seal_open($box_bin, $keypair);
32
33
		sodium_memzero($keypair);
34
		sodium_memzero($box_bin);
35
36
		if($this->message === false){
0 ignored issues
show
introduced by
The condition $this->message === false can never be true.
Loading history...
37
			throw new CryptoException('invalid box');
38
		}
39
40
		return $this;
41
	}
42
43
}
44