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

SecretBox   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

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