You are here

function encrypt_encrypt_basic in Encrypt 7

Same name and namespace in other branches
  1. 6 includes/encrypt.encrypt.inc \encrypt_encrypt_basic()

Callback for Encrypt implementation: default

This method uses a simple encryption method of character replacement.

1 string reference to 'encrypt_encrypt_basic'
encrypt_encrypt_method_info in includes/encrypt.encrypt.inc
Implementation of hook_encrypt_method_info().

File

includes/encrypt.encrypt.inc, line 62
This file holds the hook implementation for the encrypt modules

Code

function encrypt_encrypt_basic($op = 'encrypt', $text = '', $options = array()) {
  $processed_text = '';

  // Check for and assign key
  $key = !empty($options['key']) ? $options['key'] : variable_get('drupal_private_key', 'no_key');

  // Caching length operations to speed up for loops.
  $text_length = strlen($text);
  $key_length = strlen($key);

  // Loop through each character.
  for ($i = 0; $i < $text_length; $i++) {
    $char = substr($text, $i, 1);
    $keychar = substr($key, $i % $key_length - 1, 1);

    // Encrypt or decrypt the character.
    if ($op == 'decrypt') {
      $char = chr(ord($char) - ord($keychar));
    }
    else {
      $char = chr(ord($char) + ord($keychar));
    }
    $processed_text .= $char;
  }
  return $processed_text;
}