SignedMessage::open()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * Class SignedMessage
4
 *
5
 * @filesource   SignedMessage.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_sign, sodium_crypto_sign_open, sodium_memzero;
16
17
use const SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES, SODIUM_CRYPTO_SIGN_SECRETKEYBYTES;
18
19
class SignedMessage extends CryptoBoxAbstract{
20
21
	/** @inheritdoc */
22
	public function create(string $message):CryptoBoxInterface{
23
		$this->checkKeypair(SODIUM_CRYPTO_SIGN_SECRETKEYBYTES);
24
25
		$this->box = sodium_crypto_sign($this->checkMessage($message), $this->keypair->secret);
26
27
		sodium_memzero($message);
28
29
		return $this;
30
	}
31
32
	/**
33
	 * @inheritdoc
34
	 *
35
	 * @throws \chillerlan\Cryptobox\CryptoException
36
	 */
37
	public function open(string $box_bin):CryptoBoxInterface{
38
		$this->checkKeypair(null, SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES);
39
40
		$this->message = sodium_crypto_sign_open($box_bin, $this->keypair->public);
41
42
		sodium_memzero($box_bin);
43
44
		if($this->message !== false){
45
			return $this;
46
		}
47
48
		throw new CryptoException('invalid box'); // @codeCoverageIgnore
49
	}
50
51
}
52