SealedBox   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A open() 0 14 2
1
<?php
2
/**
3
 * Class SealedBox
4
 *
5
 * @filesource   SealedBox.php
6
 * @created      25.01.2018
7
 * @package      chillerlan\Cryptobox
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Cryptobox;
14
15
use function sodium_crypto_box_keypair_from_secretkey_and_publickey, sodium_crypto_box_seal,
16
	sodium_crypto_box_seal_open, sodium_memzero;
17
18
use const SODIUM_CRYPTO_BOX_PUBLICKEYBYTES, SODIUM_CRYPTO_BOX_SECRETKEYBYTES;
19
20
class SealedBox extends CryptoBoxAbstract{
21
22
	/** @inheritdoc */
23
	public function create(string $message):CryptoBoxInterface{
24
		$this->checkKeypair(null, SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
25
26
		$this->box = sodium_crypto_box_seal($this->checkMessage($message), $this->keypair->public);
27
28
		sodium_memzero($message);
29
30
		return $this;
31
	}
32
33
	/**
34
	 * @inheritdoc
35
	 *
36
	 * @throws \chillerlan\Cryptobox\CryptoException
37
	 */
38
	public function open(string $box_bin):CryptoBoxInterface{
39
		$this->checkKeypair(SODIUM_CRYPTO_BOX_SECRETKEYBYTES, SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
40
41
		$keypair       = sodium_crypto_box_keypair_from_secretkey_and_publickey($this->keypair->secret, $this->keypair->public);
42
		$this->message = sodium_crypto_box_seal_open($box_bin, $keypair);
43
44
		sodium_memzero($keypair);
45
		sodium_memzero($box_bin);
46
47
		if($this->message !== false){
48
			return $this;
49
		}
50
51
		throw new CryptoException('invalid box'); // @codeCoverageIgnore
52
	}
53
54
}
55