Skip to content

Commit 7bb257c

Browse files
Merge pull request #64 from TimWolla/improve-sodium-check
Improve sodium implementation performance
2 parents 5e9b582 + 93276b1 commit 7bb257c

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/Base64.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public static function encode(
5353
#[\SensitiveParameter]
5454
string $binString
5555
): string {
56-
if (extension_loaded('sodium')) {
56+
if (\extension_loaded('sodium')) {
5757
$variant = match(static::class) {
58-
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL,
59-
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE,
58+
Base64::class => \SODIUM_BASE64_VARIANT_ORIGINAL,
59+
Base64UrlSafe::class => \SODIUM_BASE64_VARIANT_URLSAFE,
6060
default => 0,
6161
};
6262
if ($variant > 0) {
6363
try {
64-
return sodium_bin2base64($binString, $variant);
64+
return \sodium_bin2base64($binString, $variant);
6565
} catch (SodiumException $ex) {
6666
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
6767
}
@@ -85,15 +85,15 @@ public static function encodeUnpadded(
8585
#[\SensitiveParameter]
8686
string $src
8787
): string {
88-
if (extension_loaded('sodium')) {
88+
if (\extension_loaded('sodium')) {
8989
$variant = match(static::class) {
90-
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING,
91-
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
90+
Base64::class => \SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING,
91+
Base64UrlSafe::class => \SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
9292
default => 0,
9393
};
9494
if ($variant > 0) {
9595
try {
96-
return sodium_bin2base64($src, $variant);
96+
return \sodium_bin2base64($src, $variant);
9797
} catch (SodiumException $ex) {
9898
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
9999
}
@@ -199,15 +199,15 @@ public static function decode(
199199
'Incorrect padding'
200200
);
201201
}
202-
if (extension_loaded('sodium')) {
202+
if (\extension_loaded('sodium')) {
203203
$variant = match(static::class) {
204-
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING,
205-
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
204+
Base64::class => \SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING,
205+
Base64UrlSafe::class => \SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
206206
default => 0,
207207
};
208208
if ($variant > 0) {
209209
try {
210-
return sodium_base642bin($encodedString, $variant);
210+
return \sodium_base642bin($encodedString, $variant);
211211
} catch (SodiumException $ex) {
212212
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
213213
}

src/Hex.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public static function encode(
4848
#[\SensitiveParameter]
4949
string $binString
5050
): string {
51-
if (extension_loaded('sodium')) {
51+
if (\extension_loaded('sodium')) {
5252
try {
53-
return sodium_bin2hex($binString);
53+
return \sodium_bin2hex($binString);
5454
} catch (SodiumException $ex) {
5555
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
5656
}
@@ -117,9 +117,9 @@ public static function decode(
117117
string $encodedString,
118118
bool $strictPadding = false
119119
): string {
120-
if (extension_loaded('sodium') && $strictPadding) {
120+
if (\extension_loaded('sodium') && $strictPadding) {
121121
try {
122-
return sodium_hex2bin($encodedString);
122+
return \sodium_hex2bin($encodedString);
123123
} catch (SodiumException $ex) {
124124
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
125125
}

0 commit comments

Comments
 (0)