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
|
|
|
* @param string $seed_bin |
19
|
|
|
* |
20
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoKeyInterface |
21
|
|
|
*/ |
22
|
|
|
protected function createBoxKeypair(string $seed_bin = null):CryptoKeyInterface{ |
23
|
|
|
return (new BoxKeypair)->create($seed_bin); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string|null $secret_bin |
28
|
|
|
* |
29
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoKeyInterface |
30
|
|
|
*/ |
31
|
|
|
protected function createBoxKeypairFromSecret(string $secret_bin = null):CryptoKeyInterface{ |
32
|
|
|
return (new BoxKeypair)->createFromSecret($secret_bin); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $seed_bin |
37
|
|
|
* |
38
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoKeyInterface |
39
|
|
|
*/ |
40
|
|
|
protected function createSignKeypair(string $seed_bin = null):CryptoKeyInterface{ |
41
|
|
|
return (new SignKeypair)->create($seed_bin); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $message |
46
|
|
|
* @param string $secret |
47
|
|
|
* @param string $public |
48
|
|
|
* @param string|null $nonce |
49
|
|
|
* |
50
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
51
|
|
|
*/ |
52
|
|
|
protected function createBox(string $message, string $secret, string $public, string $nonce = null):CryptoBoxInterface{ |
53
|
|
|
return (new Box(['keypair' => new BoxKeypair(['secret' => $secret, 'public' => $public])])) |
54
|
|
|
->create($message, $nonce); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $box |
59
|
|
|
* @param string $secret |
60
|
|
|
* @param string $public |
61
|
|
|
* @param string $nonce |
62
|
|
|
* |
63
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
64
|
|
|
*/ |
65
|
|
|
protected function openBox(string $box, string $secret, string $public, string $nonce):CryptoBoxInterface{ |
66
|
|
|
return (new Box(['keypair' => new BoxKeypair(['secret' => $secret, 'public' => $public])])) |
67
|
|
|
->open($box, $nonce); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $message |
72
|
|
|
* @param string $secret |
73
|
|
|
* @param string|null $nonce |
74
|
|
|
* |
75
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
76
|
|
|
*/ |
77
|
|
|
protected function createSecretBox(string $message, string $secret, string $nonce = null):CryptoBoxInterface{ |
78
|
|
|
return (new SecretBox(['keypair' => new BoxKeypair(['secret' => $secret])])) |
79
|
|
|
->create($message, $nonce); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $box |
84
|
|
|
* @param string $secret |
85
|
|
|
* @param string $nonce |
86
|
|
|
* |
87
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
88
|
|
|
*/ |
89
|
|
|
protected function openSecretBox(string $box, string $secret, string $nonce):CryptoBoxInterface{ |
90
|
|
|
return (new SecretBox(['keypair' => new BoxKeypair(['secret' => $secret])])) |
91
|
|
|
->open($box, $nonce); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $message |
96
|
|
|
* @param string $public |
97
|
|
|
* |
98
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
99
|
|
|
*/ |
100
|
|
|
protected function createSealedBox(string $message, string $public):CryptoBoxInterface{ |
101
|
|
|
return (new SealedBox(['keypair' => new BoxKeypair(['public' => $public])])) |
102
|
|
|
->create($message); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $box |
107
|
|
|
* @param string $secret |
108
|
|
|
* @param string $public |
109
|
|
|
* |
110
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
111
|
|
|
*/ |
112
|
|
|
protected function openSealedBox(string $box, string $secret, string $public):CryptoBoxInterface{ |
113
|
|
|
return (new SealedBox(['keypair' => new BoxKeypair(['secret' => $secret, 'public' => $public])])) |
114
|
|
|
->open($box); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $message |
119
|
|
|
* @param string $secret |
120
|
|
|
* |
121
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
122
|
|
|
*/ |
123
|
|
|
protected function signMessage(string $message, string $secret):CryptoBoxInterface{ |
124
|
|
|
return (new SignedMessage(['keypair' => new SignKeypair(['secret' => $secret])])) |
125
|
|
|
->create($message); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $box |
130
|
|
|
* @param string $public |
131
|
|
|
* |
132
|
|
|
* @return \chillerlan\Traits\Crypto\CryptoBoxInterface |
133
|
|
|
*/ |
134
|
|
|
protected function verifySignedMessage(string $box, string $public):CryptoBoxInterface{ |
135
|
|
|
return (new SignedMessage(['keypair' => new SignKeypair(['public' => $public])])) |
136
|
|
|
->open($box); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|