You are here

function _encrypt_decrypt in Encrypt 7

Same name and namespace in other branches
  1. 6 includes/encrypt.crypt.inc \_encrypt_decrypt()
  2. 7.3 includes/encrypt.encrypt.inc \_encrypt_decrypt()
  3. 7.2 includes/encrypt.encrypt.inc \_encrypt_decrypt()

Private Encrypt and Decrypt

Private internal function to Encrypt and Decrypt text.

Parameters

$op: Whether to encrypt or decrypt.

  • "encrypt": Encrypt text
  • "decrypt": Decrypt text

$text: Text to encrypt

$options: Array of options for encryption

$method: String name of method to use. Uses setting default if NULL

Return value

Encrypted text

2 calls to _encrypt_decrypt()
decrypt in ./encrypt.module
Decrypt
encrypt in ./encrypt.module
Encrypt

File

includes/encrypt.crypt.inc, line 162
This file holds the ffunctions necessary to encrypt and decrypt

Code

function _encrypt_decrypt($op = 'encrypt', $text = '', $options = array(), $method = NULL, $key_name = NULL) {
  $methods = encrypt_get_methods('full');
  $encryption_array = array();
  $processed = '';

  // Check op
  if ($op !== 'encrypt') {
    $op = 'decrypt';
  }

  // If decrypting we need to get method
  // TODO: The does not actually use $method or $key_name when decrypting. See 7.x-2.x for more info.
  if ($op == 'decrypt') {
    $encryption_array = unserialize($text);
    $method = $encryption_array['method'];
    $text = $encryption_array['text'];
    $key_name = $encryption_array['key_name'];
  }

  // Check text
  if ($text === '') {
    return $processed;
  }

  // Check method
  $method = encrypt_check_method($method);
  if ($method === FALSE) {
    return $processed;
  }

  // Get Key
  $key_array = encrypt_get_key($key_name);
  $key = $key_array['key'];
  $key_name = $key_array['name'];

  // Failsafe, if no key found, then use 'none' method
  if (empty($key) || $key_array == FALSE) {
    $method = 'none';
  }
  else {
    if (empty($options['key'])) {
      $options['key'] = $key;
    }
  }

  // Call callback function for encryption and decryption.
  $processed = call_user_func($methods[$method]['callback'], $op, $text, $options);

  // Check for returned value
  if (!empty($processed) && $op == 'encrypt') {
    $encryption_array = array(
      'text' => $processed,
      'method' => $method,
      'key_name' => $key_name,
    );

    // Serialize array
    $processed = serialize($encryption_array);
  }
  return $processed;
}