1313
1414namespace chillerlan \OAuth \Core ;
1515
16+ use chillerlan \Utilities \File ;
1617use DirectoryIterator ;
1718use InvalidArgumentException ;
1819use ReflectionClass ;
19- use RuntimeException ;
2020use function hash ;
21- use function random_bytes ;
22- use function realpath ;
23- use function sodium_base642bin ;
24- use function sodium_bin2base64 ;
25- use function sodium_bin2hex ;
26- use function sodium_crypto_secretbox ;
27- use function sodium_crypto_secretbox_keygen ;
28- use function sodium_crypto_secretbox_open ;
29- use function sodium_hex2bin ;
30- use function sodium_memzero ;
3121use function substr ;
3222use function trim ;
33- use const SODIUM_BASE64_VARIANT_ORIGINAL ;
34- use const SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ;
3523
3624/**
3725 * Common utilities for use with the OAuth providers
3826 */
3927class Utilities{
4028
41- final public const ENCRYPT_FORMAT_BINARY = 0b00 ;
42- final public const ENCRYPT_FORMAT_BASE64 = 0b01 ;
43- final public const ENCRYPT_FORMAT_HEX = 0b10 ;
44-
4529 /**
4630 * Fetches a list of provider classes in the given directory
4731 *
4832 * @return array<string, array<string, string>>
4933 */
5034 public static function getProviders (string |null $ providerDir = null , string |null $ namespace = null ):array {
51- $ providerDir = realpath (($ providerDir ?? __DIR__ .'/../Providers ' ));
35+ $ providerDir = File:: realpath (($ providerDir ?? __DIR__ .'/../Providers ' ));
5236 $ namespace = trim (($ namespace ?? 'chillerlan \\OAuth \\Providers ' ), '\\' );
5337 $ providers = [];
5438
55- if ($ providerDir === false ){
56- throw new InvalidArgumentException ('invalid $providerDir ' );
57- }
58-
5939 foreach (new DirectoryIterator ($ providerDir ) as $ e ){
6040
6141 if ($ e ->getExtension () !== 'php ' ){
@@ -79,70 +59,4 @@ public static function getProviders(string|null $providerDir = null, string|null
7959 return $ providers ;
8060 }
8161
82- /**
83- * Creates a new cryptographically secure random encryption key (in hexadecimal format)
84- */
85- public static function createEncryptionKey ():string {
86- return sodium_bin2hex (sodium_crypto_secretbox_keygen ());
87- }
88-
89- /**
90- * encrypts the given $data with $key, $format output [binary, base64, hex]
91- *
92- * @see \sodium_crypto_secretbox()
93- * @see \sodium_bin2base64()
94- * @see \sodium_bin2hex()
95- */
96- public static function encrypt (string $ data , string $ keyHex , int $ format = self ::ENCRYPT_FORMAT_HEX ):string {
97- $ nonce = random_bytes (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
98- $ box = sodium_crypto_secretbox ($ data , $ nonce , sodium_hex2bin ($ keyHex ));
99-
100- $ out = match ($ format ){
101- self ::ENCRYPT_FORMAT_BINARY => $ nonce .$ box ,
102- self ::ENCRYPT_FORMAT_BASE64 => sodium_bin2base64 ($ nonce .$ box , SODIUM_BASE64_VARIANT_ORIGINAL ),
103- self ::ENCRYPT_FORMAT_HEX => sodium_bin2hex ($ nonce .$ box ),
104- default => throw new InvalidArgumentException ('invalid format ' ), // @codeCoverageIgnore
105- };
106-
107- sodium_memzero ($ data );
108- sodium_memzero ($ keyHex );
109- sodium_memzero ($ nonce );
110- sodium_memzero ($ box );
111-
112- return $ out ;
113- }
114-
115- /**
116- * decrypts the given $encrypted data with $key from $format input [binary, base64, hex]
117- *
118- * @see \sodium_crypto_secretbox_open()
119- * @see \sodium_base642bin()
120- * @see \sodium_hex2bin()
121- */
122- public static function decrypt (string $ encrypted , string $ keyHex , int $ format = self ::ENCRYPT_FORMAT_HEX ):string {
123-
124- $ bin = match ($ format ){
125- self ::ENCRYPT_FORMAT_BINARY => $ encrypted ,
126- self ::ENCRYPT_FORMAT_BASE64 => sodium_base642bin ($ encrypted , SODIUM_BASE64_VARIANT_ORIGINAL ),
127- self ::ENCRYPT_FORMAT_HEX => sodium_hex2bin ($ encrypted ),
128- default => throw new InvalidArgumentException ('invalid format ' ), // @codeCoverageIgnore
129- };
130-
131- $ nonce = substr ($ bin , 0 , SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
132- $ box = substr ($ bin , SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
133- $ data = sodium_crypto_secretbox_open ($ box , $ nonce , sodium_hex2bin ($ keyHex ));
134-
135- sodium_memzero ($ encrypted );
136- sodium_memzero ($ keyHex );
137- sodium_memzero ($ bin );
138- sodium_memzero ($ nonce );
139- sodium_memzero ($ box );
140-
141- if ($ data === false ){
142- throw new RuntimeException ('decryption failed ' ); // @codeCoverageIgnore
143- }
144-
145- return $ data ;
146- }
147-
14862}
0 commit comments