Completed
Push — master ( 1df8ee...ec3914 )
by smiley
01:50
created

CryptoTrait::setKeypair()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Trait CryptoTrait
4
 *
5
 * @filesource   CryptoTrait.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
trait CryptoTrait{
16
17
	/**
18
	 * @var \chillerlan\Traits\Crypto\CryptoKeyInterface
19
	 */
20
	private $cryptoKeyInterface;
21
22
	/**
23
	 * @param string|null $secret_hex
24
	 * @param string|null $public_hex
25
	 *
26
	 * @return $this
27
	 */
28
	protected function setBoxKeypair(string $secret_hex = null, string $public_hex = null){
29
		return $this->setCryptoKeyInterface(BoxKeypair::class, $secret_hex, $public_hex);
30
	}
31
32
	/**
33
	 * @param string|null $secret_hex
34
	 * @param string|null $public_hex
35
	 *
36
	 * @return $this
37
	 */
38
	protected function setSignKeypair(string $secret_hex = null, string $public_hex = null){
39
		return $this->setCryptoKeyInterface(SignKeypair::class, $secret_hex, $public_hex);
40
	}
41
42
	/**
43
	 * @param \chillerlan\Traits\Crypto\CryptoKeyInterface $keypair
44
	 *
45
	 * @return $this
46
	 */
47
	protected function setKeypair(CryptoKeyInterface $keypair){
48
		unset($this->cryptoKeyInterface);
49
50
		$this->cryptoKeyInterface = $keypair;
51
52
		return $this;
53
	}
54
55
	/**
56
	 * @param string      $type_fqcn
57
	 * @param string|null $secret_hex
58
	 * @param string|null $public_hex
59
	 *
60
	 * @return $this
61
	 */
62
	private function setCryptoKeyInterface(string $type_fqcn, string &$secret_hex = null, string &$public_hex = null){
63
		unset($this->cryptoKeyInterface);
64
65
		$this->cryptoKeyInterface = new $type_fqcn([
66
			'secret' => !empty($secret_hex) ? sodium_hex2bin($secret_hex) : null,
67
			'public' => !empty($public_hex) ? sodium_hex2bin($public_hex) : null,
68
		]);
69
70
		if($secret_hex !== null){
71
			sodium_memzero($secret_hex);
72
		}
73
74
		if($public_hex !== null){
75
			sodium_memzero($public_hex);
76
		}
77
78
		return $this;
79
	}
80
81
	/**
82
	 * @param string $seed_bin
83
	 *
84
	 * @return \chillerlan\Traits\Crypto\CryptoKeyInterface
85
	 */
86
	protected function createBoxKeypair(string $seed_bin = null):CryptoKeyInterface{
87
		return (new BoxKeypair)->create($seed_bin);
88
	}
89
90
	/**
91
	 * @param string|null $secret_bin
92
	 *
93
	 * @return \chillerlan\Traits\Crypto\CryptoKeyInterface
94
	 */
95
	protected function createBoxKeypairFromSecret(string $secret_bin = null):CryptoKeyInterface{
96
		return (new BoxKeypair)->createFromSecret($secret_bin);
0 ignored issues
show
Bug introduced by
It seems like $secret_bin can also be of type null; however, parameter $secret_bin of chillerlan\Traits\Crypto...air::createFromSecret() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
		return (new BoxKeypair)->createFromSecret(/** @scrutinizer ignore-type */ $secret_bin);
Loading history...
97
	}
98
99
	/**
100
	 * @param string $seed_bin
101
	 *
102
	 * @return \chillerlan\Traits\Crypto\CryptoKeyInterface
103
	 */
104
	protected function createSignKeypair(string $seed_bin = null):CryptoKeyInterface{
105
		return (new SignKeypair)->create($seed_bin);
106
	}
107
108
	/**
109
	 * @param string      $message
110
	 * @param string|null $nonce
111
	 *
112
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
113
	 */
114
	protected function createBox(string $message, string $nonce = null):CryptoBoxInterface{
115
		return (new Box(['keypair' => $this->cryptoKeyInterface]))->create($message, $nonce);
116
	}
117
118
	/**
119
	 * @param string $box
120
	 * @param string $nonce
121
	 *
122
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
123
	 */
124
	protected function openBox(string $box, string $nonce):CryptoBoxInterface{
125
		return (new Box(['keypair' => $this->cryptoKeyInterface]))->open($box, $nonce);
126
	}
127
128
	/**
129
	 * @param string      $message
130
	 * @param string|null $nonce
131
	 *
132
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
133
	 */
134
	protected function createSecretBox(string $message, string $nonce = null):CryptoBoxInterface{
135
		return (new SecretBox(['keypair' => $this->cryptoKeyInterface]))->create($message, $nonce);
136
	}
137
138
	/**
139
	 * @param string $box
140
	 * @param string $nonce
141
	 *
142
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
143
	 */
144
	protected function openSecretBox(string $box, string $nonce):CryptoBoxInterface{
145
		return (new SecretBox(['keypair' => $this->cryptoKeyInterface]))->open($box, $nonce);
146
	}
147
148
	/**
149
	 * @param string $message
150
	 *
151
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
152
	 */
153
	protected function createSealedBox(string $message):CryptoBoxInterface{
154
		return (new SealedBox(['keypair' => $this->cryptoKeyInterface]))->create($message);
155
	}
156
157
	/**
158
	 * @param string $box
159
	 *
160
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
161
	 */
162
	protected function openSealedBox(string $box):CryptoBoxInterface{
163
		return (new SealedBox(['keypair' => $this->cryptoKeyInterface]))->open($box);
164
	}
165
166
	/**
167
	 * @param string $message
168
	 *
169
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
170
	 */
171
	protected function signMessage(string $message):CryptoBoxInterface{
172
		return (new SignedMessage(['keypair' => $this->cryptoKeyInterface]))->create($message);
173
	}
174
175
	/**
176
	 * @param string $box
177
	 *
178
	 * @return \chillerlan\Traits\Crypto\CryptoBoxInterface
179
	 */
180
	protected function verifySignedMessage(string $box):CryptoBoxInterface{
181
		return (new SignedMessage(['keypair' => $this->cryptoKeyInterface]))->open($box);
182
	}
183
184
}
185