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

SignKeypair::create()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
/**
3
 * Class SignKeypair
4
 *
5
 * @filesource   SignKeypair.php
6
 * @created      24.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 SignKeypair extends CryptoKeypair{
16
17
	/** @inheritdoc */
18
	public function create(string $seed_bin = null):CryptoKeyInterface{
19
20
		if($seed_bin !== null && strlen($seed_bin) !== SODIUM_CRYPTO_SIGN_SEEDBYTES){
21
			throw new CryptoException('invalid seed length');
22
		}
23
24
		$keypair = $seed_bin
25
			? sodium_crypto_sign_seed_keypair($seed_bin)
26
			: sodium_crypto_sign_keypair();
27
28
		$this->keypair = $keypair;
29
		$this->secret  = sodium_crypto_sign_secretkey($keypair);
30
		$this->public  = sodium_crypto_sign_publickey($keypair);
31
32
		sodium_memzero($keypair);
33
34
		if($seed_bin !== null){
35
			sodium_memzero($seed_bin);
36
		}
37
38
		return $this;
39
	}
40
41
}
42