-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.php
More file actions
131 lines (115 loc) · 3.27 KB
/
Copy pathcrypto.php
File metadata and controls
131 lines (115 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace todo\encryption;
class crypto {
/**
* @var string
*/
private $nonce;
/**
* Method to construct instance of Crypto
*
* @param string $nonce Nonce to crypt string
* @return void
*/
public function __construct(string $nonce = '') {
$this->nonce = !empty($nonce) ? base64_decode($nonce) : $this->generateNonce();
}
/**
* Method to encode string
*
* @param string $key Key to encode
* @param mixed $input Input to crypt
* @return string
*/
public function encode(string $key, string $input): string {
$keyDecode = base64_decode($key);
$keypair1_public = $this->getPublic($keyDecode);
$keypair1_secret = $this->getSecret($keyDecode);
return $this->_encode(serialize($input), $keypair1_public, $keypair1_secret);
}
/**
* Method to decode string
*
* @param string $key Key to decode
* @param string $input String to decode
* @return mixed
*/
public function decode(string $key, string $input) {
$keyDecode = base64_decode($key);
$keypair1_public = $this->getPublic($keyDecode);
$keypair1_secret = $this->getSecret($keyDecode);
return unserialize($this->_decode($input, $keypair1_public, $keypair1_secret));
}
/**
* Method to generate new key
*
* @return string
*/
public function generateKey(): string {
return base64_encode(sodium_crypto_box_keypair());
}
/**
* Method to generate new key
*
* @return string
*/
public static function getKey(): string {
return base64_encode(sodium_crypto_box_keypair());
}
/**
* Method to generate new nonce
*
* @return string
*/
public function generateNonce(): string {
return random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
}
public function getNonce(): string {
return base64_encode($this->nonce);
}
public function setNonce(string $nonce): void {
$this->nonce = base64_decode($nonce);
}
/**
* Method to decode string
*
* @param string $input Input to decode
* @param string $keyPublic Key public
* @param string $keySecret Key secret
* @return string
*/
private function _decode(string $input, string $keyPublic, string $keySecret) {
$decryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(base64_decode($keySecret), base64_decode($keyPublic));
return sodium_crypto_box_open(base64_decode($input), $this->nonce, $decryption_key);
}
/**
* Method to encode string
*
* @param string $input Input to encode
* @param string $keyPublic Key public
* @param string $keySecret Key secret
* @return string
*/
private function _encode(string $input, string $keyPublic, string $keySecret) {
$encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(base64_decode($keySecret), base64_decode($keyPublic));
return base64_encode(sodium_crypto_box($input, $this->nonce, $encryption_key));
}
/**
* Method to return secret of key
*
* @param string $key
* @return string
*/
private function getSecret(string $key) {
return base64_encode(sodium_crypto_box_secretkey($key));
}
/**
* Method to return public of key
*
* @param string $key
* @return string
*/
private function getPublic(string $key) {
return base64_encode(sodium_crypto_box_publickey($key));
}
}