You are here

public function UbercartEncryption::encrypt in Ubercart 7.3

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/classes/encrypt.inc, line 99
Utility class definition.

Class

UbercartEncryption
Handles encryption of credit-card information.

Code

public 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 < drupal_strlen($source); $i++) {
    $char1 = drupal_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;
}