SignKeypair   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 21 5
1
<?php
2
/**
3
 * Class SignKeypair
4
 *
5
 * @filesource   SignKeypair.php
6
 * @created      24.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_keypair, sodium_crypto_sign_publickey, sodium_crypto_sign_secretkey,
16
	sodium_crypto_sign_seed_keypair, sodium_memzero, strlen;
17
18
use const SODIUM_CRYPTO_SIGN_SEEDBYTES;
19
20
class SignKeypair extends CryptoKeypairAbstract{
21
22
	/**
23
	 * @inheritdoc
24
	 *
25
	 * @throws \chillerlan\Cryptobox\CryptoException
26
	 */
27
	public function create(string $seed_bin = null):CryptoKeypairInterface{
28
29
		if($seed_bin !== null && strlen($seed_bin) !== SODIUM_CRYPTO_SIGN_SEEDBYTES){
30
			throw new CryptoException('invalid seed length');
31
		}
32
33
		$keypair = $seed_bin
34
			? sodium_crypto_sign_seed_keypair($seed_bin)
35
			: sodium_crypto_sign_keypair();
36
37
		$this->keypair = $keypair;
38
		$this->secret  = sodium_crypto_sign_secretkey($keypair);
39
		$this->public  = sodium_crypto_sign_publickey($keypair);
40
41
		sodium_memzero($keypair);
42
43
		if($seed_bin !== null){
44
			sodium_memzero($seed_bin);
45
		}
46
47
		return $this;
48
	}
49
50
}
51