BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.class.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. class Crypto
  4. {
  5. /**
  6. * Encrypts input text for use in database
  7. *
  8. * @param string $plaintext
  9. * @return encrypted string or false if DB key not accessible
  10. */
  11. public static function encrypt($plaintext)
  12. {
  13. if (apcu_exists('DBKEY')) {
  14. $iv_size = openssl_cipher_iv_length('aes-256-cbc');
  15. $iv = openssl_random_pseudo_bytes($iv_size);
  16. $ret = base64_encode($iv.openssl_encrypt($plaintext, 'aes-256-cbc', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv));
  17. return $ret;
  18. } else {
  19. return false;
  20. }
  21. }
  22. /**
  23. * Decrypts input text from database
  24. *
  25. * @param string $ciphertext
  26. * @return decrypted string string or false if DB key not accessible
  27. */
  28. public static function decrypt($ciphertext)
  29. {
  30. if (apcu_exists('DBKEY')) {
  31. $iv_size = openssl_cipher_iv_length('aes-256-cbc');
  32. $iv = substr(base64_decode($ciphertext), 0, $iv_size);
  33. $ciphertext = substr(base64_decode($ciphertext), $iv_size);
  34. return openssl_decrypt($ciphertext, 'aes-256-cbc', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv);
  35. } else {
  36. return false;
  37. }
  38. }
  39. }