| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Cryptography; |
| 4 |
|
| 5 |
class Cryptography |
| 6 |
{ |
| 7 |
public static $sodiumCompat; |
| 8 |
|
| 9 |
public static function getSodiumCompat() |
| 10 |
{ |
| 11 |
if (!self::$sodiumCompat) { |
| 12 |
self::$sodiumCompat = new SodiumCompat(); |
| 13 |
} |
| 14 |
return self::$sodiumCompat; |
| 15 |
} |
| 16 |
|
| 17 |
public static function encrypt($message, $key) |
| 18 |
{ |
| 19 |
// check $key length and slice if key is longer than 32 bytes |
| 20 |
if (strlen($key) > 32) { |
| 21 |
$key = substr($key, 0, 32); |
| 22 |
} |
| 23 |
return base64_encode(self::getSodiumCompat()->compatEncrypt($message, $key)); |
| 24 |
} |
| 25 |
|
| 26 |
public static function decrypt($message, $key) |
| 27 |
{ |
| 28 |
if (strlen($key) > 32) { |
| 29 |
$key = substr($key, 0, 32); |
| 30 |
} |
| 31 |
return self::getSodiumCompat()->compatDecrypt(base64_decode($message), $key); |
| 32 |
} |
| 33 |
} |
| 34 |
|