You are here

public function MarstonEncryption::encrypt in Ubercart 8.4

Encrypts plaintext.

Parameters

string $key: Key used for encryption.

string $plaintext: Text string to be encrypted.

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

Return value

string Cyphertext. String containing encrypted text.

Overrides EncryptionInterface::encrypt

File

uc_store/src/MarstonEncryption.php, line 38

Class

MarstonEncryption
Deprecated. Handles encryption of credit-card information.

Namespace

Drupal\uc_store

Code

public function encrypt($key, $plaintext, $sourcelen = 0) {
  $this->errors = [];

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

    // Commented out to prevent errors getting logged for use cases that may
    // have variable encryption/decryption requirements.

    /* $this->errors[] = t('No value has been supplied for encryption'); */
    return;
  }
  while (strlen($plaintext) < $sourcelen) {
    $plaintext .= ' ';
  }
  $target = NULL;
  $factor2 = 0;
  for ($i = 0; $i < mb_strlen($plaintext); $i++) {
    $char1 = mb_substr($plaintext, $i, 1);
    $num1 = strpos(self::$scramble1, $char1);
    if ($num1 === FALSE) {
      $this->errors[] = t('Source string contains an invalid character (@char)', [
        '@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;
}