|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\Security; |
|
10
|
|
|
|
|
11
|
|
|
use Waca\SiteConfiguration; |
|
12
|
|
|
|
|
13
|
|
|
class EncryptionHelper |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SiteConfiguration |
|
17
|
|
|
*/ |
|
18
|
|
|
private $configuration; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* EncryptionHelper constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param SiteConfiguration $configuration |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(SiteConfiguration $configuration) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->configuration = $configuration; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function encryptData($secret) |
|
31
|
|
|
{ |
|
32
|
|
|
$iv = openssl_random_pseudo_bytes(16); |
|
33
|
|
|
$password = $this->getEncryptionKey(); |
|
34
|
|
|
$encryptedKey = openssl_encrypt($secret, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv); |
|
35
|
|
|
|
|
36
|
|
|
$data = base64_encode($iv) . '|' . base64_encode($encryptedKey); |
|
37
|
|
|
|
|
38
|
|
|
return $data; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function decryptData($data) |
|
42
|
|
|
{ |
|
43
|
|
|
list($iv, $encryptedKey) = array_map('base64_decode', explode('|', $data)); |
|
44
|
|
|
|
|
45
|
|
|
$password = $this->getEncryptionKey(); |
|
46
|
|
|
|
|
47
|
|
|
$secret = openssl_decrypt($encryptedKey, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv); |
|
48
|
|
|
|
|
49
|
|
|
return $secret; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
private function getEncryptionKey() |
|
56
|
|
|
{ |
|
57
|
|
|
return openssl_digest($this->configuration->getTotpEncryptionKey(), 'sha256'); |
|
58
|
|
|
} |
|
59
|
|
|
} |