-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathHmacEncrypter.php
More file actions
153 lines (129 loc) · 4.16 KB
/
Copy pathHmacEncrypter.php
File metadata and controls
153 lines (129 loc) · 4.16 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Authentication\HMAC;
use CodeIgniter\Encryption\EncrypterInterface;
use CodeIgniter\Encryption\Exceptions\EncryptionException;
use CodeIgniter\Shield\Auth;
use CodeIgniter\Shield\Config\AuthToken;
use CodeIgniter\Shield\Exceptions\RuntimeException;
use Config\Encryption;
use Config\Services;
use Exception;
/**
* HMAC Encrypter class
*
* This class handles the setup and configuration of the HMAC Encryption
*/
class HmacEncrypter
{
/**
* Codeigniter Encrypter
*
* @var array<string, EncrypterInterface>
*/
private array $encrypter;
/**
* Auth Token config
*/
private readonly AuthToken $authConfig;
/**
* Constructor
* Setup encryption configuration
*/
public function __construct()
{
$this->authConfig = config('AuthToken');
$this->getEncrypter($this->authConfig->hmacEncryptionCurrentKey);
}
/**
* Decrypt
*
* @param string $encString Encrypted string
*
* @return string Raw decrypted string
*
* @throws EncryptionException
*/
public function decrypt(string $encString): string
{
$matches = [];
// check for a match
if (preg_match('/^\$b6\$(\w+?)\$(.+)\z/', $encString, $matches) !== 1) {
throw new EncryptionException('Unable to decrypt string');
}
$encrypter = $this->getEncrypter($matches[1]);
return $encrypter->decrypt(base64_decode($matches[2], true));
}
/**
* Encrypt
*
* @param string $rawString Raw string to encrypt
*
* @return string Encrypted string
*
* @throws EncryptionException
* @throws RuntimeException
*/
public function encrypt(string $rawString): string
{
$currentKey = $this->authConfig->hmacEncryptionCurrentKey;
$encryptedString = '$b6$' . $currentKey . '$' . base64_encode($this->encrypter[$currentKey]->encrypt($rawString));
if (strlen($encryptedString) > $this->authConfig->secret2StorageLimit) {
throw new RuntimeException('Encrypted key too long. Unable to store value.');
}
return $encryptedString;
}
/**
* Check if the string already encrypted
*/
public function isEncrypted(string $string): bool
{
return preg_match('/^\$b6\$/', $string) === 1;
}
/**
* Check if the string already encrypted with the Current Set Key
*/
public function isEncryptedWithCurrentKey(string $string): bool
{
$currentKey = $this->authConfig->hmacEncryptionCurrentKey;
return preg_match('/^\$b6\$' . $currentKey . '\$/', $string) === 1;
}
/**
* Generate Key
*
* @return string Secret Key in base64 format
*
* @throws Exception
*/
public function generateSecretKey(): string
{
return base64_encode(random_bytes($this->authConfig->hmacSecretKeyByteSize));
}
/**
* Retrieve encrypter for selected key
*
* @param string $encrypterKey Index Key for selected Encrypter
*/
private function getEncrypter(string $encrypterKey): EncrypterInterface
{
if (! isset($this->encrypter[$encrypterKey])) {
if (! isset($this->authConfig->hmacEncryptionKeys[$encrypterKey]['key'])) {
throw new RuntimeException('Encryption key does not exist.');
}
$config = new Encryption();
$config->key = $this->authConfig->hmacEncryptionKeys[$encrypterKey]['key'];
$config->driver = $this->authConfig->hmacEncryptionKeys[$encrypterKey]['driver'] ?? $this->authConfig->hmacEncryptionDefaultDriver;
$config->digest = $this->authConfig->hmacEncryptionKeys[$encrypterKey]['digest'] ?? $this->authConfig->hmacEncryptionDefaultDigest;
$this->encrypter[$encrypterKey] = Services::encrypter($config);
}
return $this->encrypter[$encrypterKey];
}
}