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

Box::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
/**
3
 * Class Box
4
 *
5
 * @filesource   Box.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
16
class Box extends CryptoBox{
17
18
	/**
19
	 * @param string      $message
20
	 * @param string|null $nonce_bin
21
	 *
22
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
23
	 */
24
	public function create(string $message, string $nonce_bin = null):CryptoBoxInterface{
25
		$this->checkKeypair(SODIUM_CRYPTO_BOX_SECRETKEYBYTES, SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
26
		$message = $this->checkMessage($message);
27
28
		$this->nonce = $nonce_bin ?? random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
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 secret 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 public on the interface chillerlan\Traits\Crypto\CryptoKeyInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
32
		$this->box = sodium_crypto_box($message, $this->nonce, $keypair);
33
34
		sodium_memzero($keypair);
35
		sodium_memzero($message);
36
37
		if($nonce_bin !== null){
38
			sodium_memzero($nonce_bin);
39
		}
40
41
		return $this;
42
	}
43
44
	public function open(string $box_bin, string $nonce_bin):CryptoBoxInterface{
45
		$this->checkKeypair(SODIUM_CRYPTO_BOX_SECRETKEYBYTES, SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
46
47
		$keypair = sodium_crypto_box_keypair_from_secretkey_and_publickey($this->keypair->secret, $this->keypair->public);
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...
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...
48
49
		$this->message = sodium_crypto_box_open($box_bin, $nonce_bin, $keypair);
50
51
		sodium_memzero($keypair);
52
		sodium_memzero($box_bin);
53
		sodium_memzero($nonce_bin);
54
55
		if($this->message === false){
0 ignored issues
show
introduced by
The condition $this->message === false can never be true.
Loading history...
56
			throw new CryptoException('invalid box');
57
		}
58
59
		return $this;
60
	}
61
62
63
}
64