You are here

function uc_encryption_class::encrypt in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_store/uc_store.module \uc_encryption_class::encrypt()

Encrypts plaintext.

Parameters

$key: String key used for encryption.

$source: Plaintext. Text string to be encrypted.

$sourcelen: Minimum plaintext length. Plaintext $source which is shorter than $sourcelen will be padded by appending spaces.

Return value

Cyphertext. Text string containing encrypted $source.

File

uc_store/uc_store.module, line 1916
Contains global Ubercart functions and store administration functionality.

Class

uc_encryption_class
Handle encryption of credit-card information.

Code

function encrypt($key, $source, $sourcelen = 0) {
  $this->errors = array();

  // Convert key into sequence of numbers
  $fudgefactor = $this
    ->convertKey($key);
  if ($this->errors) {
    return;
  }
  if (empty($source)) {

    // Commented out to prevent errors getting logged for use cases that may
    // have variable encryption/decryption requirements. -RS
    // $this->errors[] = t('No value has been supplied for encryption');
    return;
  }
  while (strlen($source) < $sourcelen) {
    $source .= ' ';
  }
  $target = NULL;
  $factor2 = 0;
  for ($i = 0; $i < strlen($source); $i++) {
    $char1 = substr($source, $i, 1);
    $num1 = strpos(self::$scramble1, $char1);
    if ($num1 === FALSE) {
      $this->errors[] = t('Source string contains an invalid character (@char)', array(
        '@char' => $char1,
      ));
      return;
    }
    $adj = $this
      ->applyFudgeFactor($fudgefactor);
    $factor1 = $factor2 + $adj;
    $num2 = round($factor1) + $num1;
    $num2 = $this
      ->checkRange($num2);
    $factor2 = $factor1 + $num2;
    $char2 = substr(self::$scramble2, $num2, 1);
    $target .= $char2;
  }
  return $target;
}