| @@ -1,11 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Cryptography; |
| 4 | 4 | |
| 5 | -use BitCode\BitForm\Core\Util\Log; | |
| 6 | -use WP_Error; | |
| 7 | - | |
| 8 | 5 | class Cryptography |
| 9 | 6 | { |
| 10 | 7 | public static $sodiumCompat; |
| 11 | 8 | |
| @@ -18,32 +15,19 @@ | ||
| 18 | 15 | } |
| 19 | 16 | |
| 20 | 17 | public static function encrypt($message, $key) |
| 21 | 18 | { |
| 22 | - try { | |
| 23 | - if (32 !== strlen($key)) { | |
| 24 | - $key = hash('sha256', $key, true); // Generate a 32-byte raw binary key | |
| 25 | - } | |
| 26 | - return base64_encode(self::getSodiumCompat()->compatEncrypt($message, $key)); | |
| 27 | - } catch (Exception $e) { | |
| 28 | - // Handle the exception (e.g., log it, rethrow it, or return a meaningful error message) | |
| 29 | - Log::debug_log('Encryption failed: ' . $e->getMessage()); | |
| 30 | - /* translators: %s: dynamic value. */ | |
| 31 | - return new WP_Error('encryption_failed', sprintf(__('Encryption failed: %s', 'bit-form'), esc_html($e->getMessage()))); | |
| 19 | + // check $key length and slice if key is longer than 32 bytes | |
| 20 | + if (strlen($key) > 32) { | |
| 21 | + $key = substr($key, 0, 32); | |
| 32 | 22 | } |
| 23 | + return base64_encode(self::getSodiumCompat()->compatEncrypt($message, $key)); | |
| 33 | 24 | } |
| 34 | 25 | |
| 35 | 26 | public static function decrypt($message, $key) |
| 36 | 27 | { |
| 37 | - try { | |
| 38 | - if (32 !== strlen($key)) { | |
| 39 | - $key = hash('sha256', $key, true); // Generate a 32-byte raw binary key | |
| 40 | - } | |
| 41 | - return self::getSodiumCompat()->compatDecrypt(base64_decode($message), $key); | |
| 42 | - } catch (\Exception $e) { | |
| 43 | - // Handle the exception | |
| 44 | - Log::debug_log('Decryption failed: ' . $e->getMessage()); | |
| 45 | - /* translators: %s: dynamic value. */ | |
| 46 | - return new WP_Error('decryption_failed', sprintf(__('Decryption failed: %s', 'bit-form'), esc_html($e->getMessage()))); | |
| 28 | + if (strlen($key) > 32) { | |
| 29 | + $key = substr($key, 0, 32); | |
| 47 | 30 | } |
| 31 | + return self::getSodiumCompat()->compatDecrypt(base64_decode($message), $key); | |
| 48 | 32 | } |
| 49 | 33 | } |